admin管理员组文章数量:1291925
Here is a live example of my problem
/
I would have expected that the "Create" button returned an empty string, when its radio buttons are not selected. Instead it reads the value from the radio buttons from the form below.
The bottom "Save" button always returns the value from the form above. Very strange!
Here is the code
$(document).ready(function(){
$('form').live('submit', function(){
var title = this.elements.title.value;
var owner = this.elements.owner.value;
var type = $('input:radio[name=ctype]:checked').val() || '';
alert(type);
});
});
The idea is, I will auto-generate forms of the type at the bottom, and so don't know the ID's before hand, so I just give them random numbers to make them unique.
How can this problem be solved?
Here is a live example of my problem
http://jsfiddle/littlesandra88/xksB9/
I would have expected that the "Create" button returned an empty string, when its radio buttons are not selected. Instead it reads the value from the radio buttons from the form below.
The bottom "Save" button always returns the value from the form above. Very strange!
Here is the code
$(document).ready(function(){
$('form').live('submit', function(){
var title = this.elements.title.value;
var owner = this.elements.owner.value;
var type = $('input:radio[name=ctype]:checked').val() || '';
alert(type);
});
});
The idea is, I will auto-generate forms of the type at the bottom, and so don't know the ID's before hand, so I just give them random numbers to make them unique.
How can this problem be solved?
Share Improve this question asked May 11, 2011 at 12:23 Sandra SchlichtingSandra Schlichting 26k38 gold badges121 silver badges185 bronze badges 1-
My version of chrome treats your example HTML very strangely. I think you should either put your table inside your form, and/or your forms inside
td
s - interlacing the form and table as you are doing causes my browser to prematurely and automatically terminate your form DOM elements leaving the radio buttons outside the forms. – sje397 Commented May 11, 2011 at 12:41
3 Answers
Reset to default 8You may want to just change this line:
var type = $('input:radio[name=ctype]:checked').val() || '';
to
var type = $('#create_form input:radio[name=ctype]:checked').val() || '';
So that jQuery only looks in the top form, rather than in whole DOM.
Not sure what the overall goal is, but that edit at least gets you the behavior you're looking for.
If you want the individual save buttons to function in a similar way, change it to:
var type = $(this).find('input:radio[name=ctype]:checked').val() || '';
EDIT
To get the other forms to work, you need to change the HTML a bit so that the form tags wrap the tables, and not the other way around. This is what I changed each for to (notice that this would make multiple tables instead of one table):
<form action="" method="post">
<input name="anchor" value="54" type="hidden">
<table class="alerts" cellspacing="0">
<tbody>
<tr>
<td class="activity-data">54</td>
<td class="activity-data"> <input name="title" id="54_title" value="" type="text" /> </td>
<td class="activity-data"> <input name="owner" id="54_owner" value="" type="text" /> </td>
<td class="activity-data"> <input name="ctype" value="individuel" type="radio" checked/> Individuel <br> <input name="ctype" value="course" type="radio" /> Course </td>
<td> <input value="Save" type="submit" /></td>
</tr>
</tbody>
</table>
</form>
This way, the radio buttons are proper children of the forms and jQuery is happy about that.
Each group of radio buttons should probably have their own matching names, because it looks like the form treats them as separate groups. For instance, your first group of radio buttons might have the name foo
:
<input name="foo" id="ctype" value="individuel" type="radio" /> Individuel <br/>
<input name="foo" id="ctypee" value="course" type="radio" /> Course <br/>
Now these radio buttons are a part of the same radio group, and the others are a part of another group (although they should be named different from each other as well). Now you can safely grab the value from the foo
radio group:
$(document).ready(function(){
$('form').live('submit', function(){
var title = this.elements.title.value;
var owner = this.elements.owner.value;
// looking for 'foo' radio button group selection
var type = $('input:radio[name=foo]:checked').val() || '';
alert(type); // alerts the correct selection from the top form
});
});
If you look at this line:
<input name="ctype" value="individuel" type="radio" checked/>
you can see that you've included a 'checked' attribute in there. Remove it to solve your issue around having the 'individuel' returned when there are no selections.
Oh, and if you want to pre-select a value, it's supposed to be
<input name="ctype" value="individuel" type="radio" checked="checked" />
NOTE: This is just to add to what others have said already.
本文标签: javascriptAlways getting wrong radio button value What39s wrongStack Overflow
版权声明:本文标题:javascript - Always getting wrong radio button value. What's wrong? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741542393a2384397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论