jQuery .prop("disabled", false) 未启用输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17469077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:09:58  来源:igfitidea点击:

jQuery .prop("disabled", false) is not enabling an input

javascriptjqueryhtmljquery-mobilecordova

提问by RichardMc

I'm having a problem re-enabling an input after it has been disabled on the page load.

在页面加载时禁用输入后,我在重新启用输入时遇到问题。

I'm using two inputs to accept two dates, but I want it that the second input is not enabled until the first one has a value.

我使用两个输入来接受两个日期,但我希望第二个输入在第一个输入有值之前不启用。

<div id="date1" data-role="fieldcontain">
    <label for="date1-start" id="date1-startlbl">Start Date</label>
    <input name="date1-start" id="date1-start" type="date" data-role="datebox"data-options='{"mode": "calbox", "overrideDateFormat": "%Y-%m-%d", "beforeToday": true}' data-theme="a" onchange="showdate()"/>
    <label for="date1-end" id="date1-endlbl">End Date</label>   
    <input name="date1-end" id="date1-end" type="date" data-role="datebox" data-options='{"mode": "calbox", "overrideDateFormat": "%Y-%m-%d", "beforeToday": true}' data-theme="a" onchange="datechart()"/>
</div>

The second input is disabled successfully on the page load with.

第二个输入在页面加载时成功禁用。

$("#date1-end").prop("disabled", true);

The first date has an onchangeevent that calls the following function

第一个日期有一个onchange调用以下函数的事件

function showdate(){
    if ($("#date1-start").val() != null){
        if ($("#date1-end").val() != ""){
            datechart();
        }
        else{
            $("#date1-end").prop("disabled", false);
            $("#date1-end").trigger('change');
        }
    }
}

The function is called and prop("disabled", false)when reached fires without any errors, but the input remains disabled. The trigger('change') I am using is an attempt at refreshing the element but the same problem exists without it.

该函数被调用,prop("disabled", false)到达时会触发而没有任何错误,但输入保持禁用状态。我正在使用的 trigger('change') 是尝试刷新元素,但没有它也存在同样的问题。

I have tried different variations of .attrfrom an older version of Jquery but I also get the same problem.

我尝试.attr了旧版本的 Jquery 的不同变体,但我也遇到了同样的问题。

I am using Jquery 1.9.1, Jquery-mobile 1.3.1 and PhoneGap 2.7.0

我正在使用 Jquery 1.9.1、Jquery-mobile 1.3.1 和 PhoneGap 2.7.0

采纳答案by Gajotres

Working example: http://jsfiddle.net/d9vzs/

工作示例:http: //jsfiddle.net/d9vzs/

This is not how you enable disable jQUery Mobile input elements.

这不是您启用禁用 jQUEry Mobile 输入元素的方式。

It can be done with this functions:

可以通过以下功能完成:

$( "input" ).textinput( "disable" );


$( "input" ).textinput( "enable" );

Official documentation: http://api.jquerymobile.com/textinput/#method-disable

官方文档:http: //api.jquerymobile.com/textinput/#method-disable

回答by ElmoVanKielmo

Had the same problem some time ago:

前段时间遇到同样的问题:

$("#date1-end").prop("disabled", null);

@Gautam3164's answer is also good.

@Gautam3164 的回答也不错。

回答by Gautam3164

Try with removeAttrlike

尝试removeAttr喜欢

$("#date1-end").removeAttr("disabled");

You can also try with proplike

你也可以试试prop

$("#date1-end").prop("disabled",false);

回答by Chirag Vidani

Prop is used just to get the value and set the value. Refer this http://api.jquery.com/prop/

Prop 仅用于获取值和设置值。请参阅此http://api.jquery.com/prop/

$("#date1-end").prop("disabled", false);