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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:05:09  来源:igfitidea点击:

Redirect to a page according to selected option from radio button through 'onclick' , without 'submit' the form in php

javascripthtmlforms

提问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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="radio" onClick="test()" name="choice" value="pc">PC &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="ps2">PS2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="ps3">PS3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="psp">PSP &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onclick="idForm()"  name="choice" value="pc"/>PC &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="ps2"/>PS2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="ps3"/>PS3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="psp"/>PSP &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>
</body>

Jsfiddlelink

Jsfiddle链接

回答by iam batman

You can get the value of the option by adding onchangeevent 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.locationin your test()method use this window.location=yourpage.phpinstead of this window.open('pc.php','_self')

对于重定向,您可以window.location在您的test()方法中使用 thiswindow.location=yourpage.php而不是 thiswindow.open('pc.php','_self')