2011-05-25 50 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); 
    }); 
相關問題