jQuery—如何执行';每个';选定元素上的函数?

jQuery—如何执行';每个';选定元素上的函数?,jquery,jquery-selectors,each,Jquery,Jquery Selectors,Each,我想禁用屏幕上的所有按钮 我试过这个: $(":button").each(function () { $(this).attr("disabled", true); }); 它不起作用,我想我没有正确使用“这个”这个词 编辑:按钮位于表格单元格中 我尝试过类似的方法,但不起作用: $("#gameboard-table tbody tr td button").attr("disabled", true); 对于标记 $("button").attr("disabled", tru

我想禁用屏幕上的所有按钮

我试过这个:

$(":button").each(function () {
    $(this).attr("disabled", true);
});
它不起作用,我想我没有正确使用“这个”这个词

编辑:按钮位于表格单元格中

我尝试过类似的方法,但不起作用:

$("#gameboard-table tbody tr td button").attr("disabled", true);
对于
标记

$("button").attr("disabled", true);
$("input[type=button]").attr("disabled", true);
对于
标记

$("button").attr("disabled", true);
$("input[type=button]").attr("disabled", true);
对于JQuery 1.6+将
attr
更改为
prop

或者简单地用普通javascript编写禁用行

$("button, input[type=button]").each(function () {
    this.disabled=true;
});
更新:

var options = {
    type: "POST",
    //...
    //more ajax vars 
    //...
    success: function (response) {

        //...
        //update your table with the buttons,
        //...

        //disable buttons using one of the methods from above:
        $("button, input[type=button]").each(function () {
            this.disabled=true;
        });
    }
};
$.ajax(options);
如果要禁用动态创建的按钮,请确保在创建按钮后运行代码,而不是在页面加载前/加载时运行代码

如果您使用的是Ajax,则需要在使用按钮更新表后禁用Ajax成功函数上的按钮

下面是您需要做的一个示例:

var options = {
    type: "POST",
    //...
    //more ajax vars 
    //...
    success: function (response) {

        //...
        //update your table with the buttons,
        //...

        //disable buttons using one of the methods from above:
        $("button, input[type=button]").each(function () {
            this.disabled=true;
        });
    }
};
$.ajax(options);
无需使用
each()
。简直可以用

$(":button").attr("disabled", "disabled");

x
Y
x
$(“:按钮”).attr(“禁用”、“禁用”);
//$(“按钮”).attr(“禁用”,真);
请尝试下面的代码

//for <button> tags --> pls uncomment below line 
      $(":button").attr("disabled", "disabled");



    //Even your code works !!!! ---> uncomment below lines to see

    $(":button").each(function () {
       //$(this).attr("disabled", true);
    });
//对于标签-->请取消第行下方的注释
$(“:按钮”).attr(“禁用”、“禁用”);
//甚至你的代码也能工作!!!!->取消行下面的注释以查看
$(“:按钮”)。每个(函数){
//$(this.attr(“disabled”,true);
});
据我所知,即使在桌子里面它也能工作

:

编辑:

请尝试下面的小提琴和代码

:

//对于标签-->请取消第行下方的注释
//$(“:按钮”).attr(“禁用”、“禁用”);
$(“:按钮”)。单击(函数(){
//警报(this.id);
})
$(“.btn1”)。单击(函数(){
$(“#mytable”).append(“+”新按钮“+”);
})
//甚至你的代码也能工作!!!!->取消行下面的注释以查看
jQuery('body')。在(“click”、“#click5”,function()上{
$(“.btn”)。每个(函数(){
$(this.attr(“disabled”,true);
});
})
jQuery('body')。在(“click”、“#click6”上,函数(){
$(“.btn”)。每个(函数(){
$(this.attr(“已禁用”,false);
});
})
以下是动态生成的div click不起作用的原因:


$(“:button”).prop(“已禁用”,true)检查我的答案并使用我的JSFIDLE链接来重现问题。您还可以检查控制台或firebug调试器来判断它抛出了什么错误吗?您是否包括jQuery?该代码是否适用于dynamic div?谢谢!它正在处理不在容器内的按钮。我忘了提到按钮在桌子上。我已经编辑了我的帖子。如果我的JSFIDLE链接有助于您理解您的问题,请告诉我。谢谢。它在工作,但不在我的桌子上。我的表是动态创建的,但我仅在创建后运行此代码。动态创建的表可能是问题所在吗?嗯,好吧,让我动态尝试一下,而不是我尝试过的,它只在我开始使用页面时才起作用,但在$(文档)上不起作用。ready函数,你知道为什么吗?此函数仅在加载表后执行。这样,它将永远无法在文档就绪时工作。您需要禁用Ajax成功函数上的按钮。我将用一个例子更新我的答案,如果动态元素是在ajax的成功函数中创建的,那么是的,它会工作。只要代码是在创建动态元素之后运行的,它就会工作