PHP JavaScript - 更改选择时重定向

PHP JavaScript - Redirect when a select is changed

本文关键字:选择 重定向 JavaScript PHP      更新时间:2023-09-26
 <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>

我希望当选择 id="guitatype" 值更改为"PayPal"时,站点会将用户重定向到 ex: stackoverflow.com ,但这些脚本不起作用。

在此处打印.png

你需要关闭你的选择标签,你的JavaScript中有一些语法错误。此外,无需使用 getElementById 重新选择元素,只需将值传递到函数中:

<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");
    } 
}

你缺少 == 而不是 =

将代码更改为,

if (document.getElementById("guitatype").value == "Paypal";) {
    window.location.replace("http://stackoverflow.com");
}