简体   繁体   English

jQuery prop()方法有效,但未向元素添加“禁用”属性

[英]jQuery prop() method working, but don't added attribute “disabled” to element

I'm trying to disable a button for prevent adding new rows in table. 我试图禁用一个按钮,以防止在表中添加新行。 My problem is that prop() method work fine, but doesn't disable button (I can't add new rows, its worked fine, but button still click and looks like "active" (and attribute "disabled" not added to my button)). 我的问题是prop()方法可以正常工作,但不能禁用按钮(我不能添加新行,它可以正常工作,但是按钮仍然单击并且看起来像“活动”(并且属性“已禁用”未添加到我的按钮))。

If I use attr("disabled", "disabled") instead of prop("disabled", true) that works fine and button disabling (attribute "disabled" added to element), but I read what to use attr() is bad after JQuery 1.6+. 如果我使用attr("disabled", "disabled")而不是prop("disabled", true)可以正常工作并禁用按钮(属性“ disabled”添加到元素),但是我读到使用attr()不好在JQuery 1.6+之后。

var addBtn = $(".k-grid-add");
addBtn.bind("click", function () {
    if (grid.dataSource.data().length == 9) {
        $(addBtn).prop("disabled", true);          // <- work, but button still "active"
        // $(addBtn).attr("disabled", "disabled"); // <- work fine
    }

What i doing wrong? 我做错了什么? Thanks! 谢谢!

由于disabled是布尔属性,因此需要将其设置为disabled="disabled"

$(addBtn).prop("disabled", "disabled");

It is working for me ! 它为我工作! Just look what I tested .... 看看我测试过的....

 addBtn = $("#btn"); addBtn.prop("disabled", true); addBtn.on("click",function() { console.log(999); }); // enable.onclick = function() { addBtn.prop("disabled", false); } // disable.onclick = function() { addBtn.prop("disabled", true); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="btn">sdf</button> <button id="enable">Enable</button> <button id="disable">Disable</button> 

I can't verify that, it should work. 我无法验证,它应该可以工作。 Here's a part my code, which is working, if I enter more than 6 characters, the buttons are active, if I delete it the buttons get disabled. 这是我的代码的一部分,它可以正常工作,如果我输入的字符数超过6个,则按钮处于活动状态,如果删除它,则按钮将被禁用。 What version of jQuery do you use? 您使用什么版本的jQuery? Also, the use of bind is deprecated in jQuery version >3.0, using element.on() will be more future proof ( http://api.jquery.com/bind/ ). 另外,在jQuery版本> 3.0中不推荐使用bind,使用element.on()会更适合将来使用( http://api.jquery.com/bind/ )。

$commentsInput.on('input.UECommenting', function (event) {
    let buttonsDisabled = $commentsInput.val().length < 7 ? true : false;
    for (let button in $commentingButtons) {
        $commentingButtons[button].prop({ 'disabled': buttonsDisabled, 'title': buttonsDisabled ? 'No comments without content!' : '' });
    }
});

And I have to recommend getting used to use namespaces for events, with that you can easily use element.off('.namespace') to ensure that your events wont be setted and fired multiple times. 而且我还建议您习惯使用事件的名称空间,这样您就可以轻松使用element.off('.namespace')来确保不会多次设置和触发您的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM