javascript 根据从单选按钮通过 'onclick' 选择的选项重定向到页面,无需在 php 中“提交”表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25824094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to a page according to selected option from radio button through 'onclick' , without 'submit' the form in php
提问by Pawan
This is the code which is not working according to my requirement
这是不符合我的要求的代码
<head>
<script type="text/javascript">
function test(){
if(document.form.choice.value='pc'){
window.open('pc.php','_self');
return true;
}
else if(document.form.choice.value='ps2'){
window.open('ps2.php','_self');
return true;
}
else if(document.form.choice.value='ps3'){
window.open('ps3.php','_self');
return true;
}
else if(document.form.choice.value='psp'){
window.open('psp.php','_self');
return true;
}
}
</script>
</head>
<body>
Games<br /><br />
<form name="form">ALL
<input type="radio" onClick="test()" name="choice" value="pc">PC
<input type="radio" onClick="test()" name="choice" value="ps2">PS2
<input type="radio" onClick="test()" name="choice" value="ps3">PS3
<input type="radio" onClick="test()" name="choice" value="psp">PSP
</form>
</body>
Also i want to know that how to display the field automatically just by selecting the selected field in Drop down list without 'submit'
另外我想知道如何在没有“提交”的情况下通过在下拉列表中选择选定的字段来自动显示该字段
<select name="genre">
<option value="">--Select Genre and Click Go--</option>
<option value="All">All</option>
<option value="Action / Mission">Action / Mission</option>
<option value="Arcade">Arcade</option>
<option value="Racing">Racing</option>
<option value="Sports">Sports</option>
<option value="Kids">Kids</option>
<option value="Strategy">Strategy</option>
<option value="Adventure">Adventure</option>
</select>
回答by Perlica
Try this...
试试这个...
EDITED:
编辑:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script type="text/javascript">
function idForm(){
var selectvalue = $('input[name=choice]:checked', '#idForm').val();
if(selectvalue == "pc"){
window.open('http://www.google.com','_self');
return true;
}
else if(selectvalue == "ps2"){
window.open('http://www.google.com','_self');
return true;
}else if(selectvalue == 'ps3'){
window.open('http://www.google.com','_self');
return true;
}else if(selectvalue == 'psp'){
window.open('http://www.google.com','_self');
return true;
}
return false;
};
</script>
</head>
<body>
Games<br /><br />
<form id="idForm">ALL
<input type="radio" onclick="idForm()" name="choice" value="pc"/>PC
<input type="radio" onclick="idForm()" name="choice" value="ps2"/>PS2
<input type="radio" onclick="idForm()" name="choice" value="ps3"/>PS3
<input type="radio" onclick="idForm()" name="choice" value="psp"/>PSP
</form>
</body>
Jsfiddlelink
Jsfiddle链接
回答by iam batman
You can get the value of the option by adding onchange
event in the <select>
tag.
您可以通过onchange
在<select>
标签中添加事件来获取选项的值。
<select name="genre" onchange="selectOption()">
<option value="">--Select Genre and Click Go--</option>
<option value="All">All</option>
<option value="Action / Mission">Action / Mission</option>
<option value="Arcade">Arcade</option>
<option value="Racing">Racing</option>
<option value="Sports">Sports</option>
<option value="Kids">Kids</option>
<option value="Strategy">Strategy</option>
<option value="Adventure">Adventure</option>
</select>
In your script add this
在你的脚本中添加这个
function selectOption(){
//each option value
var option=document.form.genre.value;
alert(option);
}
For redirecting you can use window.location
in your test()
method
use this window.location=yourpage.php
instead of this window.open('pc.php','_self')
对于重定向,您可以window.location
在您的test()
方法中使用 thiswindow.location=yourpage.php
而不是 thiswindow.open('pc.php','_self')