简体   繁体   English

如何从输入提交字段中删除 disabled = "disabled" 属性?

[英]How to remove disabled = "disabled" attribute from input submit field?

I'm trouble having to remove the disabled="disabled" attribute from an input field.我很难从输入字段中删除disabled="disabled"属性。

Here are the three jQuery methods that I tried:以下是我尝试过的三种 jQuery 方法:

 jQuery('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').removeAttr('disabled'); jQuery('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').prop('disabled', false); jQuery('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').removeProp('disabled');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="submit" value="Mark Complete" class="abcd" disabled="disabled">

But haven't been able to remove the disabled="disabled" attribute.但一直无法删除disabled="disabled"属性。 Can any one tell what's wrong or how could I remove disabled attribute ?任何人都可以告诉我出了什么问题,或者我怎么能删除 disabled 属性?

Thanks谢谢

Let's look at the selector...让我们看看选择器...

.db_single_lesson .ld-breadcrumbs:first input[type=submit]

This code is looking for some element with a db_single_lesson , containing another element with a ld-breadcrumbs class, whose first input is a submit type.这段代码正在寻找一些带有db_single_lesson元素,其中包含另一个带有ld-breadcrumbs类的元素,其第一个输入是提交类型。 You do not have these two containing classes.您没有这两个包含类。 I have added them, and your first attempted solution with removeAttr() now works fine...我已经添加了它们,你第一次尝试使用removeAttr()解决方案现在工作正常......

 jQuery('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').removeAttr('disabled');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="db_single_lesson"> <div class="ld-breadcrumbs"> <input type="submit" value="Mark Complete" class="abcd" disabled="disabled"> </div> </div>

The proper way to enable a button in jQuery would be:在 jQuery 中启用按钮的正确方法是:

$('proper selector').prop('disabled', true);

it looks like you not selecting a button correctly, try to add id for selection to test the solution by itself.看起来您没有正确选择按钮,请尝试添加 id 以供选择以自行测试解决方案。 Next instead - also for testing - change maybe color on the button to ensure that you select it correctly.接下来 - 也用于测试 - 更改按钮上的颜色以确保您正确选择它。

Based on your selectors I would try根据您的选择器,我会尝试

$('input.abcd').prop('disabled', true);

For selection of the first element can be used .first()可以使用.first()来选择第一个元素

 jQuery('.db_single_lesson .ld-breadcrumbs input[type=submit]').first().removeAttr('disabled');
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div class="db_single_lesson"> <div class="ld-breadcrumbs"> <input type="submit" value="Mark Complete" class="abcd" disabled="disabled"> </div> <div class="ld-breadcrumbs"> <input type="submit" value="Mark Complete" class="abcd" disabled="disabled"> </div> </div>

Try to call尝试打电话

$('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').prop('disabled', '');

This should remove the disabled tag from your element.这应该从您的元素中删除禁用的标签。

This assumes that .db_single_lesson .ld-breadcrumbs:first input[type=submit] is the correct selector for your desired element.这假设.db_single_lesson .ld-breadcrumbs:first input[type=submit]是您所需元素的正确选择器。 It is not clear from your question if this is correct for your button.从您的问题中不清楚这对您的按钮是否正确。

From your question only the line could look like this从你的问题来看,只有这条线看起来像这样

$('input[type=submit]').prop('disabled', '');

There wasn't an issue with the selectors.选择器没有问题。 I was getting length=1 for the selector.我得到了选择器的length=1 The problem was that some other script was overriding mine jquery script and hence it was not working.问题是其他一些脚本覆盖了我的 jquery 脚本,因此它不起作用。 So I enclosed my script in the setTimeout function with a delay to get the desired result.所以我将我的脚本包含在setTimeout函数中,并delay获得所需的结果。

setTimeout(function(){
        jQuery('.db_single_lesson .ld-breadcrumbs:first input[type=submit]').removeAttr('disabled');
    }, 500);

Thanks谢谢

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

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