【问题标题】:How to select a input with name and value如何选择具有名称和值的输入
【发布时间】:2014-04-23 23:44:39
【问题描述】:

我需要在 jquery 中选择一个带有名称和值的单选输入

在此示例中,如何选择名称为 SiblingSex 且值为 female 的元素

<form action="">
<input type="radio"  name="SiblingSex" value="male">Male<br>
<input type="radio"  name="SiblingSex" value="female">Female


<input type="radio"  name="ParentSex" value="male">Male<br>
<input type="radio"  name="ParentSex" value="female">Female

</form>

我需要一些类似的东西

$('input[name="SiblingSex"]' /*GENDER SELECTOR */)

【问题讨论】:

  • 做这样的事情没有任何意义!使用val() 获得价值为什么需要它作为选择器?
  • @DhavalMarthak 在我看来,OP 只想拥有female value of SiblingSex 而不是来自female from ParentSex
  • @Jai 如果是,那么只有name 选择器就足够了!不是吗?
  • 但这也会让你获得男性的价值。
  • @DhavalMarthak 当然我不想得到 val ,这只是一个例子,我已经编辑了问题

标签: javascript jquery jquery-selectors attributes


【解决方案1】:

你可以用这个添加另一个属性选择器:

$('input[name="SiblingSex"][value="female"]').val();

无论是否检查,上述行每次都会为您提供值。

因此,如果您也只想在检查时获得该值,请添加:checked

$('input[name="SiblingSex"][value="female"]:checked').val();

【讨论】:

  • 很好,它的工作原理,我正在尝试类似$('input[name="SiblingSex",value="female"]').val() 的东西,我错过了 []
  • @vimalraj.s 你也应该添加:checked
【解决方案2】:

看看我JS Fiddle Code上的演示吧

显示两种情况:

1) when you want the value without selecting radio button.


2) when you want value after selecting radio button.

或者你想要的东西在这里::

JS Fiddle Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-06
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 2013-01-05
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多