检查div中的复选框或单选按钮是否已选中

San*_*mar 1 jquery

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

例如:

     <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>
Run Code Online (Sandbox Code Playgroud)

按钮点击我想显示问题,并在那个问题的答案面前.如下

你叫什么名字= abc

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

谢谢.

ank*_*kur 7

在这里你去一些代码示例

 var chk = $('#dvTest input:radio:checked');
 chk.attr('value');
Run Code Online (Sandbox Code Playgroud)

它会给你检查哪个无线电按钮.无论你想要什么价值,请将其保存在该值的属性中:

 <input type="radio" name="r1" checked="checked" value="abc"/>
Run Code Online (Sandbox Code Playgroud)

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


Ant*_*Ant 5

请参阅http://api.jquery.com/checked-selector/了解如何使用 :checked 选择器。