简体   繁体   English

用onclick改变Javascript变量

[英]Javascript variable change with onclick

I want to have a variable changed within a checkbox that causes an if statement to compute without reloading the page, but I'm struggling... still learning, sorry! 我希望在一个复选框中更改一个变量,导致if语句在不重新加载页面的情况下进行计算,但我很难...仍在学习,对不起!

Here's the relevant code: 这是相关的代码:

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" onclick="lettuce=true" name="food" value="lettuce" /> Lettuce<br />

 <--the if statement-->
 <script type="text/javascript">    
      if (lettuce == true) {
    document.write("45");   
  }
 </script>

Thanks! 谢谢!

<input type="checkbox" onchange="lettuce=this.checked; recompute();" name="food" value="lettuce" /> Lettuce<br />

<script type="text/javascript">
function recompute()    
      if (lettuce == true) {
    document.write("45");   
  }
}
 </script>

First, you want the onchange event, which is called when the state of the checkbox changes. 首先,您需要onchange事件,该事件在复选框的状态更改时调用。 The this.checked returns the boolean value (true or false) depending on that state. this.checked根据该状态返回布尔值(true或false)。 Second, the if statement is only processed once, when the page loads that script. 其次,当页面加载该脚本时, if语句只处理一次。 Place it in a function to call it again. 将它放在一个函数中再次调用它。

Wrap the if statement into a function and then call the function in the onclick event. 将if语句包装到函数中,然后在onclick事件中调用该函数。

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" onclick="my_function();" name="food" value="lettuce" /> Lettuce<br />

 <!--the if statement-->
 <script type="text/javascript">
      function my_function() {
          if (lettuce == true) {
              lettuce = false;
          } else {
              lettuce = true;
              document.write("45");
          }
      }
 </script>

First, your logic will never set lettuce to false. 首先,你的逻辑永远不会将生菜变为假。 I understand that we should eat more lettuce, but we should still give the user a choice ;) 我知道我们应该吃更多的生菜,但我们仍然应该给用户一个选择;)

Secondly, try to keep your Javascript out of the html: 其次,尝试将您的Javascript保持在html之外:

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" id="lettuce" name="food" value="lettuce" /> Lettuce<br />

 <script type="text/javascript">  
      document.getElementById('lettuce').addEventListener('click',checkLettuce,false);
      function checkLettuce(){ 

          if (document.getElementById('lettuce').checked === true) {
             lettuce = true;
             document.write("45");  
          } 
          else
          {
             lettuce = false;
          }
      }
 </script>

Working demo: http://jsfiddle.net/AlienWebguy/renut/ 工作演示: http//jsfiddle.net/AlienWebguy/renut/

You need to call a function in your onclick, because the script you have now: 您需要在onclick中调用一个函数,因为您现在拥有的脚本:

if(lettuce == true){
    document.write("45");   
}

Executes before your onclick ever occurs. 在你的onclick发生之前执行。

Try this: 尝试这个:

 <!--The following line is within a form-->
 <input type="checkbox" onclick="clicked(true);" name="food" value="Lettuce" /> Lettuce<br />
 <input type="checkbox" onclick="clicked(false);" name="food" value="Carrot" /> Carrot<br />
 <input type="checkbox" onclick="clicked(false);" name="food" value="Cool Beans" /> Cool Beans<br />
 <input type="checkbox" onclick="clicked(false);" name="food" value="Pepper" /> Pepper<br />
 <--the if statement-->
 <script type="text/javascript">    
    function clicked(lettuce){
         if(lettuce)
             alert('You toggled Lettuce!');
         else
             alert('You toggled some other vegetable!');
    }
 </script>

Setting lettuce=true wont cause your if statement to run. 设置lettuce=true不会导致你的if语句运行。 You just need to write a function that will run when your checkbox is clicked. 您只需要编写一个在单击复选框时运行的函数。

<input type="checkbox" onclick="functionName()".../>Lettuce</br/>

<script type="text/javascript">
function functionName(){
    document.write("45");
}
</script> 

functionName() is called when the check box is clicked. 单击复选框时将调用functionName()。

Seeing javascript functions being invoked inline from an html element makes me cringe a little. 看到从html元素内联调用的javascript函数让我有点畏缩。

<input type="checkbox" id="chkLettuce" name="food" value="lettuce" /> Lettuce<br />

 <--the if statement-->

 <script type="text/javascript"> 
   var chkLettuce = document.getElementById("chkLettuce");
   chkLettuce.onclick = chkLettuce.click = function() {
      lettuce = !!chkLettuce.checked;
      if(lettuce){
         alert('45 or whatever');
      }
   }
 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM