简体   繁体   English

单击时如何更改复选框的“选中”属性?

[英]How do I change the “checked” attribute of a check box when clicked?

I am currently trying to build a list with checkboxes that can be easily checked/unchecked to be included or excluded. 我目前正在尝试建立一个带有复选框的列表,这些复选框可以轻松地选中/取消选中以包含或排除。 When the page loads most of the list items will have the checkbox "checked" attribute to show that the item is included. 当页面加载时,大多数列表项将具有复选框“ checked”属性,以显示该项被包括在内。 Im trying to change the checkbox attribute when the user changes the selection so that I can save the results. 我试图在用户更改选择时更改复选框属性,以便我可以保存结果。

I Have tried a few combinations that I have found here on Stackoverflow but none are changing the attribute, Im not sure what Im doing wrong. 我已经尝试了一些在Stackoverflow上找到的组合,但是都没有更改属性,我不确定我做错了什么。

 function check() { if ($("#working").prop("checked", true)) { $("#working").prop("checked", false) } else { $("#working").prop("checked", true) } } var data = { id: document.getElementById('workingId').value, visible: document.getElementById('working').checked }; $(document).on("click", ".save", function() { alert(data); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="hidden" id="workingId" value="13245"> <input type="checkbox" name="working" id="working" onclick="check(this);" checked> I am a check box <button type="button" class="btn save"> Save</button> 

I am hoping to print an array that has an ID for the checkbox (12345) and whether the checkbox is now checked/unchecked. 我希望打印一个数组,该数组具有复选框的ID(12345),以及该复选框是否现在处于选中状态/未选中状态。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

I don't know if I understand your question right. 不知道您的问题对不对。 Is this what you want ? 这是你想要的吗 ?

 var data = {}; function check() { if ($("#working").prop("checked")) { data = { id: document.getElementById('workingId').value, visible: document.getElementById('working').checked }; } else { data = {}; } } $(document).on("click", ".save", function() { console.log(data); }); check(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="hidden" id="workingId" value="13245"> <input type="checkbox" name="working" id="working" onclick="check(this);" checked> I am a check box <button type="button" class="btn save"> Save</button> 

here's a way to do it with multiple checks: 这是通过多次检查来实现的一种方法:

 var data = {}; function check() { data = {}; $("[name=working]").each((ign, o)=>{ if ($(o).prop('checked')) data[o.id] = $(o).attr('data-val'); }); } $(document).on("click", ".save", function() { console.log(data); }); check(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" data-val="13245" name="working" id="working1" onclick="check(this);" checked> I am a check box <br> <input type="checkbox" data-val="23245" name="working" id="working2" onclick="check(this);" checked> I am other check box <br> <input type="checkbox" data-val="33245" name="working" id="working3" onclick="check(this);" checked> I am yet another check box <button type="button" class="btn save"> Save</button> 

Try this: 尝试这个:

var $checkBox = $("#working");
var data = {
  id: $("#workingId").val(),
  visible: $checkBox.is(":checked")
};

$(".save").on("click", function () {
  data.visible = $checkBox.is(":checked");
  alert(JSON.stringify(data));
});

// Do other things with the 'data' object...

No onclick="check(this);" 没有onclick="check(this);" logic is needed. 需要逻辑。

Notes: 笔记:

  • Consider switching to the latest JS. 考虑切换到最新的JS。 I see that you're still writing in ES5. 我发现您仍在使用ES5编写程序。
  • Consistency is very importing when developing. 开发时一致性非常重要。 If already importing jQuery, consider using it across the board. 如果已经导入了jQuery,请考虑全面使用它。 I see in your initial logic that you use it for some element selection and not others. 在您的初始逻辑中,我看到您将其用于某些元素选择,而不用于其他元素。
  • When accessing the DOM via JS, with or without jQuery, try to stay as DRY as possible. 通过JS(不管是否使用jQuery)访问DOM时,请尽量保持DRY。 For example, if you need to access the working div in more than one place, store the query selection in a variable. 例如,如果您需要在多个位置访问working div,则将查询选择存储在变量中。 Resource-wise, DOM access/manipulation is not as cheap as variable creation. 在资源方面,DOM访问/操作不像创建变量那样便宜。

Additionally, you can use an HTML data-* attribute in your checkbox element to store the "workingId" and remove your hidden element(s) entirely. 此外,您可以在checkbox元素中使用HTML data-*属性来存储“ workingId”并完全删除hidden元素。

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

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