简体   繁体   English

jQuery如果复选框已选中,则添加值为true

[英]jquery if checkbox checked add value true

How to make jquery click function if checkBox checked then status value set to 'true', if not cheked value set to 'false'? 如果未选中checkBox,然后将状态值设置为“ true”,如果未检查的值设置为“ false”,则如何使jquery click功能呢?

if I check checkbox 1 , then status 1 set to true if I check checkbox 2 , then status 2 set to true if I check checkbox 3 , then status 3 set to true 如果我选中复选框1,则将状态1设置为true(如果选中复选框2),然后将状态2设置为true(如果我选中复选框3),则将状态3设置为true

 <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label> <input name="status[]" type="text" value=""> </div> 

As your input is within <lable> , use parent().next() to set the value. 由于您的输入在<lable> ,因此请使用parent().next()设置值。

Here is the working example. 这是工作示例。

 $(function() { $('.form-group input[type="checkbox"]').change(function() { if ($(this).is(":checked")) { $(this).parent().next().val('true') } else $(this).parent().next().val('false'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label> <input name="status[]" type="text" value=""> </div> 

Try this : 尝试这个 :

$('input[type="checkbox"]').change(function() { 
    if($(this).prop('checked')){
       $(this).parent().next().val("true");
    }else{
       $(this).parent().next().val("false");
    }
});

  $('input[type="checkbox"]').change(function () { if ($(this).prop('checked')) { $(this).closest('label').next('input[type="text"]').val('true'); } else { $(this).closest('label').next('input[type="text"]').val('false'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="1"> Checkbox 1</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="2"> Checkbox 2</label> <input name="status[]" type="text" value=""> </div> <div class="form-group"> <label><input name="checkBox[]" type="checkbox" value="3"> Checkbox 3</label> <input name="status[]" type="text" value=""> </div> 

You can try this 你可以试试这个

<div class="form-group">    
   <input name="checkBox" type="checkbox" value="1">
   <input name="status" type="text" value="">
</div>
<div class="form-group">    
   <input name="checkBox" type="checkbox" value="2">
   <input name="status" type="text" value="">
</div>
<div class="form-group">    
   <input name="checkBox" type="checkbox" value="3">
   <input name="status" type="text" value="">
</div>

and the jquery script 和jQuery脚本

$('input[type="checkbox"]').change(function() { 
    if ($(this).is(':checked')) {
        $(this).next('input[type="text"]').val('true');
    } else {
        $(this).next('input[type="text"]').val('false');
    }
}).change();

https://jsfiddle.net/831mjzr1/ https://jsfiddle.net/831mjzr1/

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

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