简体   繁体   中英

Getting the select option value to pass into action attribute so it can pass as url and open up the website

The Url website is appearing in the address, but it's not opening the website page....

<form action=" ">
 <select name="url">
 <option value="https://www.google.com">google</option>
 <option value="https://www.yahoo.com">yahoo</option>
 </select>
 <input type="submit"/>
</form>

<script>
 function SetData(){
 var select = document.getElementById('url').value;
 var test_id = select.options[select.selectedIndex].value;
 document.form.action = test_id;
 form.submit();
  }
</script>

Have a look at this...it's just a clickhandler on the submit button which opens the selected webpage.

HTML:

<select id="url">
   <option value="https://www.google.com">Google</option>
   <option value="https://www.yahoo.com">Yahoo</option>
 </select>
<button type="button" onclick="openUrl()">Open</button>

Javascript:

function openUrl(){
   var select = document.getElementById("url");
   var url = select.options[select.selectedIndex].value;
   window.open(url,'_blank');
}

https://jsfiddle.net/kamys76g/

here i have changed some script when click on button page is redirect as on selected value and also attribute is posted in querystring

 <script>
     function SetData(){
     var select = document.getElementsByName('url')[0].value;
     document.forms[0].action = select;
     document.forms[0].submit();
     }
    </script>

<form action=" ">
 <select name="url">
 <option value="https://www.google.com">google</option>
 <option value="https://www.yahoo.com">yahoo</option>
 </select>
 <input type="submit" value="submit" onclick="SetData()"/>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM