禁用jQuery中的按钮 [英] Disable button in jQuery

查看:57
本文介绍了禁用jQuery中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面将多个按钮创建为id = 'rbutton_"+i+"'.下面是我的代码:

My page creates multiple buttons as id = 'rbutton_"+i+"'. Below is my code:

<button type='button' id = 'rbutton_"+i+"' onclick=disable(i);>Click me</button>

使用Java语言

function disable(i){
    $("#rbutton'+i+'").attr("disabled","disabled");
}

但是当我单击按钮时它不会禁用我的按钮.

But it doesn't disable my button when I click on it.

推荐答案

改为使用.prop(并清理选择器字符串):

Use .prop instead (and clean up your selector string):

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事件绑定和this代替:


But the "best practices" approach is to use JavaScript event binding and this instead:

$('.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>

http://jsfiddle.net/mblase75/2Nfu4/

这篇关于禁用jQuery中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆