Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery按钮在引用表单字段中的值时启用后,在单击时禁用_Javascript_Jquery - Fatal编程技术网

Javascript JQuery按钮在引用表单字段中的值时启用后,在单击时禁用

Javascript JQuery按钮在引用表单字段中的值时启用后,在单击时禁用,javascript,jquery,Javascript,Jquery,我对JQuery非常陌生:我问起来有点困惑,但问起来总比另一个好: 我试图禁用按钮,但在字段/文本框中有内容时启用它。 这只是实验,只是让我的脚湿在这里 或者,我可以禁用windowsload上的按钮,或者直接在表单元素上应用该属性 $(document).ready(function() { $('#btnSubmit').attr('disabled', true); $('#myForm :input').blur(function(){ if($('#myField').v

我对JQuery非常陌生:我问起来有点困惑,但问起来总比另一个好:

我试图禁用按钮,但在字段/文本框中有内容时启用它。 这只是实验,只是让我的脚湿在这里

或者,我可以禁用
windowsload
上的按钮,或者直接在表单元素上应用该属性

$(document).ready(function() {

 $('#btnSubmit').attr('disabled', true);

$('#myForm :input').blur(function(){
    if($('#myField').val() == '')
    {
        $('#btnSubmit').attr('disabled', true);
    }
    else
    {
        $('#btnSubmit').attr('disabled', false);
    }
});
});
问题是在我输入一个值并离开该字段后,该按钮被启用,但一旦我单击该按钮,它将再次被禁用:

我错过了什么


谢谢我的朋友。

这是因为
按钮也被视为输入字段

试一试

演示:

也可以使用
.prop()
而不是
.attr()
来设置禁用状态

尝试使用以下方法:

$('#btnSubmit').attr('disabled', 'disabled'); // to disable buttons
$('#btnSubmit').removeAttr('disabled'); // to enable them

试试这个,这个应该有用

HTML示例

<input type = "text" id = "txt1">
<button class = "btnSubmit">Submit</button>

*这会有用的。请尝试*

您可以尝试此操作,
onkeyup
检查文本框是否为空,以便取消按钮,否则启用按钮

HTML:

 <form id="myForm">
       <input id="myField" type="text"/>
       <input id="btnSubmit" value="hi" type="button"/>

  </form>
$(document).ready(function() {
        //Load with button disabled,since the value of the textbox will be empty initially
        $('#btnSubmit').attr('disabled', true);
        //onkeyup check if the textbox is empty or not
        $('#myForm input[type="text"]').keyup(function(){
           if($('#myField').val() == '')
             {
               $('#btnSubmit').prop('disabled', true);
             }
            else
            {
               $('#btnSubmit').prop('disabled', false);
            }
           });
          });
$(文档).ready(函数(){
$('btnSubmit').prop('disabled',true);
$('#myForm input[type=“text”]').keyup(function(){
if($('#myField').val()='')
{             
$('btnSubmit').prop('disabled',true);
}
其他的
{
$('btnSubmit').prop('disabled',false);
}
}); });


谢谢大家!不幸的是,这在“type=Submit”上不起作用。我已经尝试了一切和所有的变化,包括这一页上的一切:;但是运气不好,除非我把字体改成了按钮。谢谢。知道这个很好!这让我很痛苦
$('#myForm:input').on('keyup' , function() {

    if($.trim($('#myField').val()).length > 0){
       $('#btnSubmit').prop('disabled', false);
    }
    else {
       $('#btnSubmit').prop('disabled', true);
    }

});
 <form id="myForm">
       <input id="myField" type="text"/>
       <input id="btnSubmit" value="hi" type="button"/>

  </form>
$(document).ready(function() {
        //Load with button disabled,since the value of the textbox will be empty initially
        $('#btnSubmit').attr('disabled', true);
        //onkeyup check if the textbox is empty or not
        $('#myForm input[type="text"]').keyup(function(){
           if($('#myField').val() == '')
             {
               $('#btnSubmit').prop('disabled', true);
             }
            else
            {
               $('#btnSubmit').prop('disabled', false);
            }
           });
          });