jquery - 如何使用Jquery获取选中的复选框和相应的信息?

标签 jquery forms

我在页面上有一些带有标题的复选框,我想使用 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/

相关文章:

javascript - 在react中加载jquery依赖库

jquery - 使用 Jquery animate 将相对元素居中

javascript - 页面清除 - JQuery Mobile

django - 使 Django forms.DateField 显示使用本地日期格式

javascript - 慢速 Jquery 动画

Angular 形式控件很快就会失去焦点

php - 在较大的表单中上传文件(一个页面上的两个表单),如何同时提交两组数据(文件和表单)? (PHP)

c# - this.Controls 返回 null

php - 命中 "enter"不会在 IE8 中发布表单

javascript - 通过 PHP 连接具有相同名称的输入值