获取jQuery mobile中的选项按钮的值

gri*_*egs 8 jquery jquery-mobile

如果我有以下jQuery mobile html;

<fieldset data-role="controlgroup" data-type="horizontal" name="Smoker" id="Smoker">
  <input name="radio3" id="radio3" value="Yes" type="radio" />
  <label for="radio3">Yes</label>
  <input name="radiobuttons1" id="radio4" value="No" type="radio" />
  <label for="radio4">No</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

我以为我可以在jQuery中做到这一点;

$("#Smoker").val()
Run Code Online (Sandbox Code Playgroud)

然而,这没有结果.我实际上是否需要查询输入以查看哪一个已被检查或是否有一个很好的jQuery Mobile方法来执行此操作?

Raf*_*fay 18

尝试

$("#Smoker :radio:checked").val();
Run Code Online (Sandbox Code Playgroud)


Jac*_*ack 5

首先你的单选按钮应该具有相同的名称属性,否则选择一个不会解除对另一个,所以你应该有类似下面的内容,而不是你当前的标记

<fieldset data-role="controlgroup" data-type="horizontal" name="Smoker" id="Smoker">
  <input name="smokerRdo" id="radio3" value="Yes" type="radio" />
  <label for="radio3">Yes</label>
  <input name="smokerRdo" id="radio4" value="No" type="radio" />
  <label for="radio4">No</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

获取值的AS与常规jQuery相同,但是你试图获取fieldset的值,而不是收音机组,而是尝试

 $('input[name=smokerRdo]:checked').val()
Run Code Online (Sandbox Code Playgroud)