[英]Getting the select option value to pass into action attribute so it can pass as url and open up the website
[英]How to make the select option tag fill the action attribute with an url so it can open that website
我需要獲取選項值以填寫表單中的action屬性,以便它可以打開網站。 我的代碼不起作用。 完成過程缺少什么?
<form action=" " name="test">
<select name="url">
<option value="google">google</option>
<option value="yahoo">yahoo</option>
</select>
<input type="submit"/>
</form>
<script>
$("form").submit(function () {
var selectVal = $('select').value();
var url = '';
if(selectVal == "google") url = 'http://www.google.com';
else if(selectVal == "yahoo") url = 'http://www.yahoo.com';
if(url != ''){
window.open(url, '_blank');
}
});
</script>
也許試試這個:
$('form').on('click', '[type=submit]', function(e) {
e.preventDefault();
// Get the selection option val
var form = $(this).parents('form');
var selectVal = $form.find('select').find(':selected').val();
var url = '';
switch (selectVal) {
case 'google':
url = 'google.com';
break;
case 'yahoo':
url = 'yahoo.com';
break;
}
if (url) {
form.attr('action', url);
form.submit();
}
})
希望有幫助!
我需要獲取選項值以填寫表單中的action屬性,以便它可以打開網站。 我的代碼不起作用。 完成過程缺少什么?
<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');
var test_id = select.options[select.selectedIndex].value;
document.form.action = test_id;
form.submit();
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.