编辑:这是可行的,但不确定为什么?
$('button').each(function() {
$(this).bind(
"click",
function() {
alert($(this).val());
});
});我不知道为什么这不管用..。现在,我只是尝试用按钮值输出一个警告,但它在我的页面上不起作用。我在Firebug中没有看到任何控制台错误,也看不到阻止它工作的任何东西。
我的HTML看起来如下:
<table id="addressbooktable">
<thead>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>7892870</td>
<td><button id="button-20" class="custom-action" value="XY89" name="info">Click</button></td>
</tr>
<tr>
<td>9382098</td>
<td><button id="button-21" class="custom-action" value="XY544" name="info">Click</button></td>
</tr>
<tr>
<td>3493900</td>
<td><button id="button-22" class="custom-action" value="XY231" name="info">Click</button></td>
</tr>
</tbody>
</table>代码是这样的:
$('button').each(function() {
$(this).click(function() {
alert($(this).val());
}
});但是,点击它一点作用都没有吗?我是不是用错了?
发布于 2010-12-21 16:24:40
您可以使用这样的单击事件绑定所有按钮,不需要遍历它们。
$(function(){
$("button").click(function() {
alert($(this).attr("value"));
});
});但更精确的选择可能是:
$(function(){
$("button[id^='button-'").click(function() {
alert($(this).attr("value"));
});
});发布于 2010-12-21 16:27:18
您缺少了一个);,并确保代码位于document.ready处理程序中,如下所示:
$(function() {
$('button').each(function() {
$(this).click(function() {
alert($(this).val());
}); //missing ); here!
});
});这样,只有那些<button>元素在DOM中才能运行,$('button')才能找到它们。此外,您还可以在一组元素上运行.click(),如下所示:
$(function() {
$('button').click(function() {
alert($(this).val());
});
});发布于 2015-07-06 15:03:47
你错过了)
$('button').each(function(){
$(this).click(function(){
alert($(this).val());
});
});https://stackoverflow.com/questions/4501430
复制相似问题