如何使用If Statement [duplicate]更改JavaScript变量的值

时间:2022-08-27 17:42:27

This question already has an answer here:

这个问题在这里已有答案:

I have multiple forms in same php page. I am able to show and hide them based the drop down value selected But I have a variable called $url i want to have different value when each form loads

我在同一个php页面中有多个表单。我可以根据选择的下拉值显示和隐藏它们但是我有一个名为$ url的变量我希望每个表单加载时都有不同的值

     <script>
      function validate(){
      var first = document.getElementById("first");
      var first-val = first.options[first.selectedIndex].value;
      var second = document.getElementById("seconed");
      var secondval = second.options[second.selectedIndex].value;

if(first-val == "Rear Tire" && secondval ==  "2WD")
{
document.getElementById("RA").style.display="";
document.getElementById("RB").style.display="none";
 var test = '<?php $url ="reartire"?>
} 

else if(first-val == "Rear Tire" && secondval ==  "MFWD")
   {
    document.getElementById("RA").style.display="none";
    document.getElementById("RB").style.display="";
   var test = '<?php $url ="fronttire"?>
   }

I tried adding

我尝试添加

Inside the the first if - var test = '<?php $url ="yahoo.com"?>';

在第一个if - var test =' ';

For the second if i added if- var test = '<?php $url ="google.com"?>'; But it only sees the the value of $url inside the second if

对于第二个,如果我添加if- var test =' ';但它只能在第二个if中看到$ url的值

1 个解决方案

#1


0  

Problem may be in the value assignment of test in js using php. Either use php short tag or echo the php variable when assigning php variable/value to a js variable.

问题可能在于使用php在js中测试的值。在将php变量/值分配给js变量时,可以使用php短标记或回显php变量。

#1


0  

Problem may be in the value assignment of test in js using php. Either use php short tag or echo the php variable when assigning php variable/value to a js variable.

问题可能在于使用php在js中测试的值。在将php变量/值分配给js变量时,可以使用php短标记或回显php变量。