繁体   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