PHP JavaScript-更改选择时重定向 功能测试(){ if(document.getElementById(“guitatype”).value=“Paypal”){ window.location.replace(“http://stackoverflow.com"); } }
我希望当select id=“guitatype”值更改为“PayPal”时,站点会将用户重定向到ex:stackoverflow.com,但这些脚本不起作用PHP JavaScript-更改选择时重定向 功能测试(){ if(document.getElementById(“guitatype”).value=“Paypal”){ window.location.replace(“http://stackoverflow.com"); } },javascript,php,html,Javascript,Php,Html,我希望当select id=“guitatype”值更改为“PayPal”时,站点会将用户重定向到ex:stackoverflow.com,但这些脚本不起作用 您需要关闭select标记,并且javascript中存在一些语法错误。此外,无需使用getElementById重新选择元素,只需将值传递给函数: <select name="betrag" id="guitatype" onchange="test();"> <?PHP
您需要关闭select标记,并且javascript中存在一些语法错误。此外,无需使用getElementById重新选择元素,只需将值传递给函数:
<select name="betrag" id="guitatype" onchange="test();">
<?PHP
foreach($pscBetraege as $pscs) {
echo'<option value="'.$pscs.'">'.$pscs.'</option>';
}
?>
<script type="text/javascript">
function test(){
if (document.getElementById("guitatype").value = "Paypal";) {
window.location.replace("http://stackoverflow.com");
}
}
</script>
</select>
功能测试(val){
如果(val==“贝宝”){
window.location.replace(“http://stackoverflow.com");
}
}
您缺少的是=而不是=
将代码更改为
<select name="betrag" id="guitatype" onchange="test(this.value);">
<?php
foreach($pscBetraege as $pscs) {
echo'<option value="'.$pscs.'">'.$pscs.'</option>';
}
?>
</select>
<script type="text/javascript">
function test(val){
if (val == "Paypal") {
window.location.replace("http://stackoverflow.com");
}
}
检查值的位置是==not=。请不要将代码转储到@coderboi_89注释中
if (document.getElementById("guitatype").value == "Paypal";) {
window.location.replace("http://stackoverflow.com");
}