我的页面创建多个按钮作为 id = 'rbutton_"+i+"'
。下面是我的代码:
<button type='button' id = 'rbutton_"+i+"' onclick=disable(i);>Click me</button>
在 JavaScript 中
function disable(i){
$("#rbutton'+i+'").attr("disabled","disabled");
}
但是当我点击它时它并没有禁用我的按钮。
最佳答案
改用 .prop
(并清理您的选择器字符串):
function disable(i){
$("#rbutton_"+i).prop("disabled",true);
}
生成的 HTML:
<button id="rbutton_1" onclick="disable(1)">Click me</button>
<!-- wrap your onclick in quotes -->
但“最佳实践”方法是使用 JavaScript 事件绑定(bind)和 this
代替:
$('.rbutton').on('click',function() {
$(this).prop("disabled",true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button class="rbutton">Click me</button>
关于jquery - 在 jQuery 中禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15122526/