简体   繁体   English

复选框 - 仅在选中jquery时运行

[英]Checkbox - run only if checked jquery

I have been looking at a lot of examples on how to accomplish this but cannot get it to work. 我一直在研究很多关于如何实现这个目标的例子,但却无法让它发挥作用。 I just want to put a simple if statement within my javascript, and when I add the if, it breaks? 我只想在我的javascript中添加一个简单的if语句,当我添加if时,它会中断吗? I am getting test to report true when checked, and false when unchecked, so what am I missing? 我正在接受测试,在检查时报告为真,如果未经检查则报告为假,那么我缺少什么? - Novice user, so if this a convoluted way to do this, I apologize. - 新手用户,所以如果这是一个令人费解的方式,我道歉。

NOT WORKING: 不工作:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{
     if(test == "true")
     {
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    }    
  })
 });
 </script>

But works, but with every click when the function is written like: 但是有效,但是当函数写成时每次单击都会:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{
     //if(test == "true")
     //{
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    //}    
  })
 });
 </script>

DEMO: DEMO:

Change if(test == "true") to if(test) or if(test === true) or to if(this.checked) if(test == "true")更改为if(test)if(test === true)if(this.checked)

HTML: HTML:

<label for=input>Click Me</label>
<input id=input type=checkbox  name=1 />

SCRIPT: 脚本:

    $('input[name="1"]').click( function () {
        var test = $(this).prop('checked');
        if(test){
          alert("checkbox checked");
          var alert_id = $(this).val();
          var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
  });
     }       
 });

NOTE: 注意:

If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

you can add console.log(test); 你可以添加console.log(test); to see the value of test. 看到测试的价值。

What you were doing was comparing true == "true" a boolean with a string 你正在做的是比较true == "true"一个boolean和一个string

Read Using the Equality Operators 使用等式运算符读取

The key problem is here that you're using true as string which is why it's not working: 关键问题在于你使用true作为字符串,这就是为什么它不起作用:

So, use it as boolean: 所以,将它用作布尔值:

if(test == true) // not "true"

You can also use like below: 你也可以使用如下:

if(test) // it says if test is true

OR, 要么,

if(test == 1) // 1 for true 0 for false

Try doing: 尝试做:

<script>

    $('input[name="1"]').click(function() {
       //var test = $(this).prop('checked'); //{
         if(jQuery(this).is(":checked"))
         {
          var alert_id = $(this).val();
          var key = $('#key').val();
          $.ajax({  
           type: "POST",  
            url: "functions/database_write.php",
           data: "id1="+alert_id+"&key="+key+"&test="+test,
           success: function(resp){  
                 $( ".pop-div" ).slideDown(100,"linear");
                setTimeout(function(){
                 $( ".pop-div" ).slideUp(100,"linear");
                 }, 1000);
                 //alert(resp);
            },  
            error: function(e){  
            alert('Error: ' + e);  
          }
        }    
      })
     });
     </script>

Try it like this: 试试这样:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{

      console.log(test);// to see whats is in test

     if(test)
     {
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    }    
  })
 });
 </script>

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

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