【发布时间】:2013-08-13 06:23:53
【问题描述】:
我有一个表,其中有复选框,一些复选框被禁用。我想使用 JavaScript 或 jQuery 为所有禁用的复选框添加一个 onclick 属性。
HTML
<table class="docs_table" id="myTable2">
<tr id="node-22" class="disabled_element">
<td title="Document can't be selected.">
<input type="checkbox" name="docs" id="/root/docname" value="/root/docname" disabled />
</td>
</tr>
<tr id="node-23" class="">
<td title="">
<input type="checkbox" name="docs" id="/root/docname2" value="/root/docname2" />
</td>
</tr>
<tr id="node-24" class="disabled_element">
<td title="Document can't be selected.">
<input type="checkbox" name="docs" id="/root/docname3" value="/root/docname3" disabled />
</td>
</tr>
</table>
在上面的代码中,docname 输入框应该在下面作为 onclick。
onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&amp;name=docname'};return false;"
和docname3 应该得到以下内容
onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&amp;name=docname3'};return false;"
我尝试了什么
$('table tr td input[type="checkbox"]').each(function () {
if ($( this ).prop('disabled')) {
$( this ).closest('tr').addClass('lineThrough');
$( this ).attr('onClick','onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&name=docname'};return false;"');
}
});
但它不起作用,我也想知道如何处理不同的docname,因为在onclick语句中name=docname对于每个输入框都会有所不同。
【问题讨论】:
-
禁用的输入不会注册许多/所有事件
-
那么onclick可能会添加到最近的
<td>或<tr>。 -
当然你的代码不会工作——你没有正确引用属性值。当您可以使用
.on()正确绑定事件时,您不应该使用 jQuery 设置onclick属性 -
@ChankeyPathak 我们可以将它添加到最近的 td 中,但这与单击复选框的感觉不同吧?无论如何,您可以查看此fiddle 以获得最近的
点击。请记住在复选框外单击。 @Harry 这违背了点击复选框的目的......
标签: javascript jquery onclick inputbox