簡體   English   中英

如何使用 jquery 或 javascript 獲取單選按鈕 Id 值

[英]How to get the radio button Id value using jquery or javascript

我認為我有這段代碼。

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

在我的 Controller 發布操作結果中,我試圖獲得我正在做的這個選擇的價值

collection["selection"]

我無法獲得我檢查的單選按鈕 ID。 我正在“打開”,但我如何需要知道在我的視圖中選擇了哪個單選按鈕?

謝謝

此 function 將為您提供選定的單選按鈕值及其 ID。

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

為您的單選按鈕提供“值”屬性:

<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
        }             
    });
});

您可以忘記單選按鈕中的 id 和 put value 屬性,代碼如下所示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM