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
jQuery selecting a forms radio buttons
提问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
回答by intellidiot
回答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)); });