禁用jQuery中的按钮


359

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

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

用JavaScript

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

但是当我单击它时,它并不会禁用我的按钮。


6
'rbutton _“ + i +”'不是有效的ID。
Diodeus-James MacFarlane

如何指定编号。它是在我的javascript中的for循环中创建的。
user2047817 2013年

如何创建一个jsFiddle以便我们可以看到您在做什么?
j08691 2013年

5
您可能想要disable(this)function disable(elem) { $(elem).attr("disabled","disabled") }
JJJ

2
jQuery可以通过使用元素的索引号来寻址元素,因此,如果操作正确,甚至可能不需要ID。您可以将disable(this)用作自引用。
Diodeus-James MacFarlane 2013年

Answers:


638

使用.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事件绑定,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>

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


这是我创建的一个小提琴。请让我做错了。 jsfiddle.net/2Nfu4/3
user2047817

好。它的工作原理完全没有问题-但是请问为什么?可能是类似的东西,我不是在我的实际代码中做的!
user2047817

看起来,就像jsFiddle的设置方式一样,如果您想准确地分析正在发生的事情,则可以在浏览器中“检查元素”。
Blazemonger

1
这很好。只要确保使用的是ajax,就可以在成功和错误情况下启用按钮。不在ajax调用结束时。因为那样,它将立即被启用,并且您将根本看不到禁用。
user890332 2014年

使用jQuery 1.10.2时,它可在IE 11中使用,但不适用于Chrome版本39.0.2171.95 m。可疑的是,用于此答案的小提琴未提供jQuery 1.10.2
Alex

35

尝试以下代码:
HTML

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

功能

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

jQuery的其他解决方案

$('button').click(function(){ 
    $(this).attr("disabled","disabled");
});

演示


其他使用纯JavaScript的解决方案

<button type='button' id = 'rbutton_1' onclick="disable(1)">Click me</button>

<script>
function disable(i){
 document.getElementById("rbutton_"+i).setAttribute("disabled","disabled");
}
</script>

演示2


第二个.click()函数可在您的演示中完美运行。但是我必须使用第一个解决方案中提到的单独方法,但是它不起作用。你能帮忙吗!
user2047817

添加了带有纯JavaScript @ user2047817的第三个解决方案
Alessandro Minoccheri

29

这里有两件事,根据OP的问题,获得最高投票的答案在技术上是正确的。

简要总结为:

$("some sort of selector").prop("disabled", true | false);

但是,如果您使用的是jQuery UI(我知道不是OP,但有人到达这里可能是),则这将禁用按钮click事件,但不会使按钮按照UI样式显示为禁用。

如果您使用的是jQuery UI样式的按钮,则应通过以下方式启用/禁用该按钮:

$("some sort of selector").button("enable" | "disable");

http://api.jqueryui.com/button/#method-disable


27

我认为这是最简单的方法:

// All buttons where id contains 'rbutton_'
const $buttons = $("button[id*='rbutton_']");

//Selected button onclick
$buttons.click(function() {
    $(this).prop('disabled', true); //disable clicked button
});

//Enable button onclick
$('#enable').click(() =>
    $buttons.prop('disabled', false) //enable all buttons
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="rbutton_200">click</button>
<button id="rbutton_201">click</button>
<button id="rbutton_202">click</button>
<button id="rbutton_203">click</button>
<button id="rbutton_204">click</button>
<button id="rbutton_205">click</button>
<button id="enable">enable</button>


1
先生,非常好,即使使用自举按钮也完全可以使用。
史蒂夫·莫雷兹

25

尝试这个

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

15

禁用按钮:

$('#button_id').attr('disabled','disabled');

启用按钮:

$('#button_id').removeAttr('disabled');

13

简而言之,用HTML可以正常工作:

<button type="button" id="btn_CommitAll"class="btn_CommitAll">save</button>

在JQuery端,将此功能用于禁用按钮:

function disableButton() {
    $('.btn_CommitAll').prop("disabled", true);
}

对于启用按钮:

function enableButton() {
    $('.btn_CommitAll').prop("disabled", false);
}

就这样。


3

这是使用ajax的方法。

$("#updatebtn").click(function () {
    $("#updatebtn").prop("disabled", true);
    urlToHandler = 'update.ashx';
            jsonData = data;
            $.ajax({
                url: urlToHandler,
                data: jsonData,
                dataType: 'json',
                type: 'POST',
                contentType: 'application/json',
                success: function (data) {
                    $("#lbl").html(data.response);
                    $("#updatebtn").prop("disabled", false);
                    //setAutocompleteData(data.response);
                },
                error: function (data, status, jqXHR) {
                    alert('There was an error.');
                    $("#updatebtn").prop("disabled", false);
                }
            }); // end $.ajax

在某些版本的jQuery中,必须使用.attr('disabled', true)。我不知道该使用哪个版本,但是我必须使用的版本(已缩小并捆绑为我正在开发的应用程序的一部分)与object does not support prop
JoeBrockhaus 2014年

2

这对我有用:

<script type="text/javascript">
function change(){
    document.getElementById("submit").disabled = false;
}
</script>

2

我想在某些情况下禁用按钮,我正在使用第一种解决方案,但对我而言不起作用。但是当我使用第二个时,它起作用了。下面是浏览器控制台的输出。

1. $('#psl2 .btn-continue')。prop(“ disabled”,true)

<a class=​"btn btn-yellow btn-continue" href=​"#">​Next​</a>

2. $('#psl2 .btn-continue')。attr(“ disabled”,“ disabled”)

<a class=​"btn btn-yellow btn-continue" href=​"#" disabled=​"disabled">​Next​</a>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.