简体   繁体   English

清除文本框后如何禁用按钮?

[英]How to disable button when text box is clear?

I need to enable textbox when user start to print on it end disable button when text box is empty. 当文本框为空时,当用户开始在其上打印结束禁用按钮时,我需要启用文本框。

here is my text box and button: 这是我的文本框和按钮:

   <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button>
   <input type="text" id="newLayerName" placeholder="name" value="" /> 

Here is the code that I use: 这是我使用的代码:

function setElementsDisabled() {
    $('#newLayerName').keyup(function () {
        if ($(this).val().length != 0)
            $('#btnSaveLayer').attr('disabled', false);
        else
            $('#btnSaveLayer').attr('disabled', true);
    })
}

From function above you can see that, when user start to enter text in text box butten enabled and when the user remove from text box button disabled. 从上面的功能中可以看到,当用户开始在文本框中输入文本但被启用时,以及用户从文本框中删除时,按钮被禁用。

And it works perfect!!! 而且效果很好!!!

But when I clear button programmatically using this jquery code: 但是当我使用以下jQuery代码以编程方式清除按钮时:

  $("#newLayerName").val('');

The text from textbox is removed but, button is not disabled. 来自文本框的文本已删除,但是按钮未被禁用。

Any idea how to change my function setElementsDisabled so that when text box is empty the button is disabled? 任何想法如何更改我的功能setElementsDisabled以便在文本框为空时禁用按钮?

try use $('#newLayerName').trigger("keyup") to achive what you want. 尝试使用$('#newLayerName').trigger("keyup")获得所需的内容。 Example below. 下面的例子。

 function setElementsDisabled() { $('#newLayerName').keyup(function() { $('#btnSaveLayer').prop('disabled', $(this).val().length == 0); }) } setElementsDisabled() $("#clear").click(function() { $("#newLayerName").val('').trigger("keyup"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button> <input type="text" id="newLayerName" placeholder="name" value="" /> <button id="clear"> clear</button> 

Here is the script 这是脚本

 $("#newLayerName").keyup(function(e){ check($(this)); }) $("#newLayerName").change(function(){ check($("#newLayerName")); }) function check(obj){ if($(obj).val().length==0){ $("#btnSaveLayer").prop("disabled",true); } else{ $("#btnSaveLayer").prop("disabled",false); } } $("#newLayerName").val("Hello"); check($("#newLayerName")); setTimeout(function(){ $("#newLayerName").val("").change(); },3000) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button> <input type="text" id="newLayerName" placeholder="name" value="" /> 

You can add this 您可以添加

 if ($('#newLayerName').val().length != 0)
                $('#btnSaveLayer').attr('disabled', false);
            else
                $('#btnSaveLayer').attr('disabled', true); 

in a seperate function and call the same method programatically when you are clearing the text box 在单独的函数中,并在清除文本框时以编程方式调用相同的方法

You can manually trigger a keyup event, and also heavily optimize this code (cache buttons, one-liner disabled logic, use of prop instead of attr , reading the event's value instead of building a $(this) object, etc.) : 您可以手动触发一个keyup事件,并且还可以对该代码进行大量优化(缓存按钮, disabled一线逻辑,使用prop而不是attr ,读取事件的值而不是构建$(this)对象等):

 const $btnSaveLayer = $('#btnSaveLayer'), $newLayerName = $("#newLayerName") $('#newLayerName').keyup( e => { $btnSaveLayer.prop('disabled', !e.target.value.length); }) $("#clearBtn").click( () => { $newLayerName.val('').trigger("keyup"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button> <input type="text" id="newLayerName" placeholder="name" value="" /> <button id="clearBtn">Clear</button> 

Whenever programmatically call $("#newLayerName").val(''); 每当以编程方式调用$("#newLayerName").val(''); instead of it you can call $("#newLayerName").val('').keyup(); 代替它,您可以调用$("#newLayerName").val('').keyup(); . Trigger keyup event after clear data. 清除数据后触发keyup事件。

 $("#newLayerName").keyup(function(e){ if($(this).val().length==0){ $("#btnSaveLayer").prop("disabled",true); } else{ $("#btnSaveLayer").prop("disabled",false); } }) function programaticallyClear() { $("#newLayerName").val('').keyup(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button> <input type="text" id="newLayerName" placeholder="name" value="" /> <button id="programaticallyClearButton" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="programaticallyClear()" >programaticallyClearButton</button> 

  1. We need to use keyup event on the input element. 我们需要在输入元素上使用keyup事件。
  2. We need to check the value entered the input. 我们需要检查输入值。 Spaces should not be allowed (obvious) 不允许有空格(明显)
  3. Event will be fired on click on clear and will call the same keyup trigger 单击clear将触发事件,并将调用相同的键入触发

 $('#newLayerName').on('keyup', function() { $('#btnSaveLayer').prop('disabled', $.trim($(this).val()).length == 0); }); $("#clear").click(function() { $("#newLayerName").val('').trigger("keyup"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSaveLayer" class="ui-btn ui-icon-plus ui-btn-icon-left ui-btn-b" onclick="SaveNewLayer()" disabled>save</button> <input type="text" id="newLayerName" placeholder="name" value="" /> <button id="clear"> clear</button> 

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

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