jQuery 检查是否选择了 div 中的复选框或单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5323743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:57:42  来源:igfitidea点击:

Check if checkbox or radio button inside div is seleted

jquery

提问by Sandeep Kumar

Using jQuery I want to check if checkboxes or radio button inside a div tag is selected and get the value of selected checkbox and radio buttons.

使用 jQuery 我想检查是否选中了 div 标签内的复选框或单选按钮,并获取所选复选框和单选按钮的值。

For Example:

例如:

     <div>
        Question
        <div>
            What is your name?
        </div>
        <div>
            <input type="radio" name="r1" checked/>abc<br/>
            <input type="radio" name="r1"/>pqr<br/>
            <input type="radio" name="r1"/>lmn<br/>
        </div>
    </div>

    <div style="visibility: hidden">
        Question
        <div>
            What is your company name?
        </div>
        <div>
            <input type="checkbox" name="c1" checked/>comp1<br/>
            <input type="checkbox" name="c1"/>Comp2<br/>
            <input type="checkbox" name="c1"/>Comp3<br/>
        </div>
    </div>

on button click I want to show question and in front of that the answer of that question. Like as follows

在按钮上单击我想显示问题,并在该问题的答案之前。喜欢如下

What is your name = abc

你叫什么名字 = abc

What is your company name = comp1, comp2

你的公司名称是什么 = comp1, comp2

Thanks.

谢谢。

回答by ankur

Here you go dude some code sample

给你一些代码示例

 var chk = $('#dvTest input:radio:checked');
 chk.attr('value');

It will give you which radio buttton is checked. Whaever value you want keep that in that value property for ex:

它会给你哪个单选按钮被选中。您想要将其保留在该 value 属性中的任何值,例如:

 <input type="radio" name="r1" checked="checked" value="abc"/>

Same you can do for checkbox.

你可以为复选框做同样的事情。

回答by Ant

See http://api.jquery.com/checked-selector/on how to use the :checked selector.

有关如何使用 :checked 选择器的信息,请参阅http://api.jquery.com/checked-selector/

回答by Adam Ayres

Maybe something like this:

也许是这样的:

http://jsfiddle.net/magicaj/xFqHN/

http://jsfiddle.net/magicaj/xFqHN/

HTML:

HTML:

<div>
    Question
    <div>
        What is your name? <span id="your-name"></span>
    </div>
    <div>
        <input type="radio" name="r1" value="abc" checked="checked" />abc<br/>
        <input type="radio" name="r1" value="pqr" />pqr<br/>
        <input type="radio" name="r1" value="lmn" />lmn<br/>
    </div>
</div>

<div id="comp" style="visibility: hidden">
    Question
    <div>
        What is your company name?<span id="comp-name"></span>
    </div>
    <div>
        <input type="checkbox" name="c1" value="comp1" checked/>comp1<br/>
        <input type="checkbox" name="c1" value="Comp2" />Comp2<br/>
        <input type="checkbox" name="c1" value="Comp3" />Comp3<br/>
    </div>
</div>

JS:

JS:

$("input[name='r1']").click(function() {
    $("#your-name").html($(this).val());
    updateCompName();
    $("#comp").css("visibility", "visible");    
});

var updateCompName = function() {
    var element = $("#comp-name");
    var compNames = "";
    compCheckboxes.each(function() {
        if (this.checked === true) {
            compNames += " " + $(this).val();
        }
    });
    $("#comp-name").html(compNames);
}

var compCheckboxes = $("input[name='c1']");

compCheckboxes.click(updateCompName);

You were missing the valueattribute on your radios and checkboxes. I added spans placeholders to update the text with the current value of the radio/checkboxes.

您缺少value收音机和复选框上的属性。我添加了 spans 占位符以使用单选框/复选框的当前值更新文本。