[英]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: 笔记:
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. 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.