How to get the radio button Id value using jquery or javascript
I have this code in my view.
<%using (Html.BeginForm("X", "Y", FormMethod.Post, new { @id = "XSS" }))
{ %>
<fieldset>
<legend>Select Selection Type</legend>
<label>Default Selections:</label>
<input type="radio" id="Default Selections" name="selection" />
<br />
<label>Existing Selections:</label>
<input type="radio" id="Existing Selections" name="selection" />
</fieldset>
<input type="submit" value="submit">
<% } %>
In my Controller post Action result I am trying to get the value of this selection i am doing
collection["selection"]
I am not able to get t开发者_运维百科he radio button Id which I checked. I am getting "on" but How do I need to know which radio button was selected in my view?
Thanks
This function will give you the selected radio button value and it's Id.
$("input:radio[name=selection]").click(function() {
var value = $(this).val();
alert(value);
var id= $(this).attr('id');
alert(id);
});
You can forget about the id an put value attribute in your radiobuttons, code will look like this.
<%using (Html.BeginForm("X", "Y", FormMethod.Post, new { @id = "XSS" }))
{ %>
<fieldset>
<legend>Select Selection Type</legend>
<label>Default Selections:</label>
<input type="radio" value="Default Selections" name="selection" />
<br />
<label>Existing Selections:</label>
<input type="radio" value="Existing Selections" name="selection" />
</fieldset>
<input type="submit" value="submit">
<% } %>
Give your radio buttons the 'value' attribute:
<input type="radio" id="Default Selections" name="selection" value="default" />
<input type="radio" id="Existing Selections" name="selection" value="existing" />
You can then distinguish between them with:
$("[name=selection]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
if (selection == 'default') {
// Do something
}
else {
// Do something else
}
});
});
$("input:radio[name=selection]").click(function (){
var somval = $(this).val();
alert(somval);
});
精彩评论