jQuery 多个单选按钮
刚接触 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将使用您提供的 HTML 为您提供索引:
另一件事可能对您有帮助:检索表中的行数:
希望这对您有所帮助。
This will get you the index, using the HTML you've provided:
Another thing that may help you: retrieving the number of rows in your table:
Hope this helps you on your way.
对于您的具体问题 1 和 2,如果您为表指定了“mytable”ID,则此示例应该为您提供所需的内容:
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:
这是一些未经测试的代码,来自我的脑海:
它的作用是将 onclick 事件附加到 div 中的每个单选按钮。 onclick 将检查该组中的最后一个单选按钮是否被选中,如果是,则添加另一个元素。
就像我说的,它还没有经过测试。 我不确定选择器 (#
div_s_0 ... :last
),所以先在 Firebug 中运行一下。Here's some untested code, off the top of my head:
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.