2011-05-25 53 views
1

我在我看来有这样的代码。如何使用jquery或javascript获取单选按钮Id值

<%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"> 
<% } %> 

在我的控制器后行动的结果,我试图让这个选择的价值我做

collection["selection"] 

我不能让我检查单选按钮标识。我正在“开”,但我如何需要知道在我的视图中选择了哪个单选按钮?

感谢

回答

1

给你的单选按钮的 '价值' 属性:

<input type="radio" id="Default Selections" name="selection" value="default" />  
<input type="radio" id="Existing Selections" name="selection" value="existing" /> 

然后,您可以在它们之间有区别:

$("[name=selection]").each(function (i) { 
    $(this).click(function() { 
     var selection = $(this).val(); 
     if (selection == 'default') { 
      // Do something 
     } 
     else { 
      // Do something else 
     }    
    }); 
}); 
1

您可以对ID的看跌期权价值忘记属性在你的单选按钮中,代码将如下所示。

<%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"> 
<% } %> 
0
$("input:radio[name=selection]").click(function(){ 
    var somval = $(this).val(); 
    alert(somval); 
    }); 
4

此功能将给予您所选择的单选按钮值,并将它的ID。

$("input:radio[name=selection]").click(function() { 
     var value = $(this).val(); 
     alert(value); 
     var id= $(this).attr('id'); 
     alert(id); 
    }); 
相关问题