我在页面上有一些带有标题的复选框,我想使用 jQuery 获取所有选中的复选框以及该复选框的对应信息?
最终,我想提交一个包含用户选择的所有内容的表单。但由于表单不知道,所以我需要在用户单击打开表单的按钮时获取信息。
谢谢!
<section id="myoptions">
<label for="123">
<div class="thisineed">Want this text</div>
<div><input type="checkbox" id="123"></div>
<div class="thisaswell">This one I want as well</div>
</label>
<label for="124">
<div class="thisineed">Want this text</div>
<div><input type="checkbox" id="124"></div>
<div class="thisaswell">This one I want as well</div>
</label>
</section>
最佳答案
您可以使用它来访问选中的复选框:
$('input[type=checkbox]:checked').each(function(){
console.log(this);
});
这是给您的一个例子。有点不清楚您在寻找什么,但请检查一下:
$('#get_form').click(function () {
$('input[type=checkbox]:checked').each(function () {
console.log(my_form);
var somevalue = $(this).parent().next('.thisaswell').text();
console.log(somevalue);
$('#my_form').append('<input type="hidden" value="'+somevalue+'" />')
});
});
还有这个 demo 以我作为示例添加的表单生成隐藏输入,并在后面添加 div 中的文本。
关于jquery - 如何使用Jquery获取选中的复选框和相应的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18273886/