如何基于数组更新复选框状态?

时间:2018-09-06 14:32:14

标签: javascript jquery

我正在处理以下代码。为什么我无法基于selected数组更新chekbox状态?

var selected = [134,135,136,137,138,139,140,141,142,143,144];
$.each(selected, function(index, value){
  $("input[type='checkbox'][value"+ value.member_key+ "]").prop("checked", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle" value="133">133<br>
<input type="checkbox" name="vehicle" value="134">134<br>
<input type="checkbox" name="vehicle" value="135">135<br>
<input type="checkbox" name="vehicle" value="136">136<br>
<input type="checkbox" name="vehicle" value="137">137<br>
<input type="checkbox" name="vehicle" value="138">138<br>
<input type="checkbox" name="vehicle" value="139">139<br>
<input type="checkbox" name="vehicle" value="140">140<br>
<input type="checkbox" name="vehicle" value="141">141<br>
<input type="checkbox" name="vehicle" value="142">142<br>
<input type="checkbox" name="vehicle" value="143">143<br>
<input type="checkbox" name="vehicle" value="144">144<br>
<input type="checkbox" name="vehicle" value="145">145<br>
<input type="checkbox" name="vehicle" value="146">146<br>

3 个答案:

答案 0 :(得分:3)

传递的member_key上没有名为value的属性。由于您是在数字数组上实现.each(),因此在每次迭代中,回调函数都会将数字本身作为第二个参数。因此,只需使用value

var selected = [134,135,136,137,138,139,140,141,142,143,144];
 $.each(selected, function(index, value){
  $("input[type='checkbox'][value="+value+"]").prop("checked", true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle" value="133">133<br>
<input type="checkbox" name="vehicle" value="134">134<br>
<input type="checkbox" name="vehicle" value="135">135<br>
<input type="checkbox" name="vehicle" value="136">136<br>
<input type="checkbox" name="vehicle" value="137">137<br>
<input type="checkbox" name="vehicle" value="138">138<br>
<input type="checkbox" name="vehicle" value="139">139<br>
<input type="checkbox" name="vehicle" value="140">140<br>
<input type="checkbox" name="vehicle" value="141">141<br>
<input type="checkbox" name="vehicle" value="142">142<br>
<input type="checkbox" name="vehicle" value="143">143<br>
<input type="checkbox" name="vehicle" value="144">144<br>
<input type="checkbox" name="vehicle" value="145">145<br>
<input type="checkbox" name="vehicle" value="146">146<br>

答案 1 :(得分:0)

var selected = [134,135,136,137,138,139,140,141,142,143,144];
 $.each(selected, function(index, value){
                                       //include the = and fix the value being compared
              $("input[type='checkbox'][value="+ value+ "]").prop("checked", true);
         })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle" value="133">133<br>
<input type="checkbox" name="vehicle" value="134">134<br>
<input type="checkbox" name="vehicle" value="135">135<br>
<input type="checkbox" name="vehicle" value="136">136<br>
<input type="checkbox" name="vehicle" value="137">137<br>
<input type="checkbox" name="vehicle" value="138">138<br>
<input type="checkbox" name="vehicle" value="139">139<br>
<input type="checkbox" name="vehicle" value="140">140<br>
<input type="checkbox" name="vehicle" value="141">141<br>
<input type="checkbox" name="vehicle" value="142">142<br>
<input type="checkbox" name="vehicle" value="143">143<br>
<input type="checkbox" name="vehicle" value="144">144<br>
<input type="checkbox" name="vehicle" value="145">145<br>
<input type="checkbox" name="vehicle" value="146">146<br>

答案 2 :(得分:0)

其他答案都在

上发表评论
select  
    a.*, b.address 
from 
    customer_details  a 
left join 
    address_details b on a.street_no = b.street_no 
where 
    b.case_id = 1 

我认为这是将代码复制到stackoverflow中的错误。我猜您在实际代码中缺少的部分是单词“值”与其实际值之间的等号。

您的代码将产生

value.member_key

您需要的地方

[value133]