Home > Blockchain >  How to redirect page when select option HTML, Laravel
How to redirect page when select option HTML, Laravel

Time:06-15

I need to redirect page when one of the option selected. not all option just "request distrubitorship" option

<select required name="subject"  id="konu" >
                                <option value="" disabled>Choose Subject</option>
                                <option value="Ask a question">Ask a question</option>
                                <option value="Leave a comment">Leave a comment</option>
                                <option value="Where to buy">Where to buy</option>
                                <option value="Product Complaint">Product Complaint</option>
                                <option value="Other">Other</option>
                                <option {{ request('subject') == 'Request SDS' ? 'selected' : '' }}  value="Request SDS">Request SDS</option>
                                <option value="Distributor">Request Distributorship</option>
                            </select>

CodePudding user response:

I once needed this too and found that your question has already been answered.

If you're fine with using inline JavaScript, check out this answer: https://stackoverflow.com/a/7562129/10440010

If you prefer staying off of inline, check out this answer under the same question: https://stackoverflow.com/a/7562201/10440010

I hope this helps!

Update:

An easy way of making it work for specific options, is to make the onChange event check whether the value of the clicked option equal to something. In the snippet below, I make it work for all links starting with HTTPS.

HTML:

<select id="redirectSelect">
    <option value="NoLink" disabled>Please select</option>
    <option value="NoLinkEither">Option 1</option>
    <option value="https://www.google.com/">External Link</option>
</select>

JavaScript:

function redirect(goto) {
  var conf = confirm("Are you sure you want to go elswhere?");
  if (conf && goto != '') {
    window.location = goto;
  }
}

var selectEl = document.getElementById('redirectSelect');

selectEl.onchange = function() {
  if (this.value.startsWith('https')) {
    var goto = this.value;
    redirect(goto);
  }
};

Hopefully this helps you enough to solve your problem. If not, leave a comment and I am glad to help!

CodePudding user response:

I have a code previously woked.Here is select

<select required name="subject" id="konu" >
                                <option value="" disabled>Choose Subject</option>
                                <option value="Ask a question">Ask a question</option>
                                <option value="Leave a comment">Leave a comment</option>
                                <option value="Where to buy">Where to buy</option>
                                <option value="Product Complaint">Product Complaint</option>
                                <option value="Other">Other</option>
                                <option {{ request('subject') == 'Request SDS' ? 'selected' : '' }}  value="Request SDS">Request SDS</option>
                                <option value="Distributor">Request Distributorship</option>
                            </select>

Here it is script.

$( document ).ready(function() {
        $("#konu").change(function(){
                var value = $("#konu").val();
                if(value=="distributor"){
                    window.location = 'be-our-distributor';
                }
            })
        });

CodePudding user response:

For the second answer, the value used in your HTML differs from the value you're checking against in the JavaScript code.

In your HTML, it's Distributor and in your JavaScript code it's distributor. Mind the capital D.

  • Related