jQuery 多个单选按钮

发布于 2024-07-07 14:36:52 字数 1133 浏览 3 评论 0原文

刚接触 javascript/jquery,很难使用 this$(this) 获取当前对象。

我有一个表格,每行都有一组单选按钮,每个按钮名为s_。 默认情况下不会检查任何单选按钮:

<tr>
   <td align="left" style="width: 300px">
      <div id="div_s_0">
         <input type="radio" name="s_0" value="1" />Public
         <input type="radio" name="s_0" value="2" />Not Public
         <input type="radio" name="s_0" value="3" />Confidential
      </div>
   </td>
</tr>
<tr>
   <td align="left" style="width: 300px">
      <div id="div_s_1">
         <input type="radio" name="s_1" value="1" />Public
         <input type="radio" name="s_1" value="2" />Not Public
         <input type="radio" name="s_1" value="3" />Confidential
      </div>
   </td>
</tr>

我正在尝试编写一个 jQuery 函数,以便在用户选择单选按钮时向表中添加新行,但前提是它们当前位于表的最后一行。 我想要做的是获取单击的单选按钮的名称属性,解析它以获取行索引(即“_”之后的部分)并将其与表中的行数进行比较。 如果它们相等,则添加一个新行,否则,不执行任何操作。

我的问题是双重的,具体取决于我应该如何解决这个问题:

1)如何返回单选按钮的名称属性,或者 2)如何返回我当前所在行的行索引?

New to javascript/jquery and having a hard time with using this or $(this) to get the current object.

I have a table with a set of radio buttons on each row, each named s_<rowindex>. None of the radio buttons are checked by default:

<tr>
   <td align="left" style="width: 300px">
      <div id="div_s_0">
         <input type="radio" name="s_0" value="1" />Public
         <input type="radio" name="s_0" value="2" />Not Public
         <input type="radio" name="s_0" value="3" />Confidential
      </div>
   </td>
</tr>
<tr>
   <td align="left" style="width: 300px">
      <div id="div_s_1">
         <input type="radio" name="s_1" value="1" />Public
         <input type="radio" name="s_1" value="2" />Not Public
         <input type="radio" name="s_1" value="3" />Confidential
      </div>
   </td>
</tr>

I'm trying to write a jQuery function to add a new row to the table whenever the user selects a radio button, but only if they are currently on the last row of the table. What I'd like to do is get the name attribute of the clicked radio button, parse it to get the row index (i.e. the part after the '_') and compare it to the number of rows in the table. If they are equal, add a new row, otherwise, do nothing.

My question is twofold, depending on how I should attack this:

1) How do I return the name attribute of a radio button, OR
2) How do I return the row index of the row I am currently in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十六岁半 2024-07-14 14:36:52

这将使用您提供的 HTML 为您提供索引:

$(document).ready(function() {
    $("input:radio").click(function() {
        var index = parseInt(this.name.split('_')[1])
    });
});

另一件事可能对您有帮助:检索表中的行数:

$($("table").children()[0]).children().length

希望这对您有所帮助。

This will get you the index, using the HTML you've provided:

$(document).ready(function() {
    $("input:radio").click(function() {
        var index = parseInt(this.name.split('_')[1])
    });
});

Another thing that may help you: retrieving the number of rows in your table:

$($("table").children()[0]).children().length

Hope this helps you on your way.

前事休说 2024-07-14 14:36:52

对于您的具体问题 1 和 2,如果您为表指定了“mytable”ID,则此示例应该为您提供所需的内容:

var rowIndex = $("#mytable tr").index($(this).parents("tr"));
var inputName = $(this).attr("name");
alert("Input Name: " + inputName + "; RowIndex: " + rowIndex);

For your specific questions 1 & 2, if you gave your table an ID of "mytable", this example should give you what you're looking for:

var rowIndex = $("#mytable tr").index($(this).parents("tr"));
var inputName = $(this).attr("name");
alert("Input Name: " + inputName + "; RowIndex: " + rowIndex);
心房的律动 2024-07-14 14:36:52

这是一些未经测试的代码,来自我的脑海:

$("#div_s_0 input[type='radio']").onclick = function() {
    if ($("#div_s_0 input[type='radio']:last").attr('checked') == 'checked') {
    /* add a new element */
    }
}

它的作用是将 onclick 事件附加到 div 中的每个单选按钮。 onclick 将检查该组中的最后一个单选按钮是否被选中,如果是,则添加另一个元素。

就像我说的,它还没有经过测试。 我不确定选择器 (#div_s_0 ... :last),所以先在 Firebug 中运行一下。

Here's some untested code, off the top of my head:

$("#div_s_0 input[type='radio']").onclick = function() {
    if ($("#div_s_0 input[type='radio']:last").attr('checked') == 'checked') {
    /* add a new element */
    }
}

What that does is attach an onclick event to each of the radio buttons in the div. The onclick will check if the last radio button in that group is checked, and if so add another element.

Like I said it hasn't been tested yet. I'm unsure about the selector (#div_s_0 ... :last) so give it a run in Firebug first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文