jQuery jQuery选择表单单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6109151/
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 20:24:23  来源:igfitidea点击:

jQuery selecting a forms radio buttons

jqueryformsjquery-selectors

提问by ilyo

I have a form and I use this document.forms["form-0"].answerN[0];to select a specific radio button, however I could not manage to do it with jQuery.

我有一个表单,我用它document.forms["form-0"].answerN[0];来选择一个特定的单选按钮,但是我无法用 jQuery 来做到这一点。

I tried $('forms["form-0"]')and $('forms[0]')to get to the form but it didn't work, and both do work using the long path..

我试着$('forms["form-0"]')$('forms[0]')去的形式,但它没有工作,以及使用长路径都做的工作..

回答by diEcho

you can access like this ( jQuery v1.6)

你可以这样访问(jQuery v1.6)

$('form > input:radio').prop('id');

DEMO

演示

update

更新

As you can aceess formby nameor idattribute also like this

因为您也可以像这样通过或属性访问表单nameid

 $('form[name="firstForm"] > input:radio').prop('id');

DEMO

演示

回答by intellidiot

Try like this: $("form input:radio")

像这样尝试: $("form input:radio")

For further assistance follow thislink.

如需进一步帮助,请点击链接。

回答by sushil bharwani

form-0 is the name of the form. You have to write something like

form-0 是表单的名称。你必须写一些类似的东西

$('form[name=form-0]') to select the form.

to get the radio button you will have to move further to use children

要获得单选按钮,您必须进一步移动才能使用儿童

so

所以

$('form[name=form-0]').children('radio[name=answerN]:first-child')

hopefully this should work

希望这应该有效

回答by Sarah West

If you have a form like this:

如果你有这样的表格:

<form action="" method="post" id="myForm">
  <input type="radio" name="myRadioButton" value="0" class="radio" id="firstRadioButton" /> Value
</form>

you can select elements by their id:

您可以通过它们的 id 选择元素:

selects the radio button

选择单选按钮

$("#firstRadioButton)

selects the form

选择表格

$("#myForm")

Or select all radio buttons:

或选择所有单选按钮:

$("#myForm input.radio").each(function(){
  alert($(this).val());
});

And without using IDs:

并且不使用 ID:

HTML:

HTML:

<form action="" method="post">
    <input type="radio" value="1" /> Value 1 <br />
    <input type="radio" value="2" /> Value 2 <br />
</form>

jQuery:

jQuery:

alert($("form").first().find("input:radio").first().val());

Example: Look here

例子:看这里

回答by Ben

You can try this to reach all the forms in your page:

您可以尝试使用此方法访问页面中的所有表单:

$('forms').each(function(id){
    console.log(id,$(this));
});