jQuery禁用/启用提交按钮 [英] jQuery disable/enable submit button

查看:159
本文介绍了jQuery禁用/启用提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML:

 < input type =textname =textField/> 
< input type =submitvalue =send/>

我该如何做这样的事情:



<当文本字段为空时,应该禁用提交(disabled =disabled)。
  • 当在文本字段中输入某些内容以删除禁用的属性。

  • 如果文本字段再次变为空(文本被删除),应该再次禁用提交按钮。 b

    我试过这样的事情:

      $(document).ready(function(){
    $('input [type =submit]')。attr('disabled','disabled');
    $('input [type =text]')。change(function ){
    if($(this).val!=''){
    $('input [type =submit]')。removeAttr('disabled');
    }
    });
    });

    ...但不起作用。有任何想法吗?感谢。

    解决方案

    问题是change事件只会在焦点从输入移开时触发(例如,有人点击输入或制表符)。请尝试使用keyup:

      $(document).ready(function(){
    $(':input [ ('disabled',true);
    $('input [type =text]')。keyup(function(){
    if($(这个).val()!=''){
    $(':input [type =submit]')。prop('disabled',false);
    }
    } );
    });


    I have this html:

    <input type="text" name="textField" />
    <input type="submit" value="send" />
    

    How can I do something like this:

    • When the text field is empty the submit should be disabled (disabled="disabled").
    • When something is typed in the text field to remove the disabled attribute.
    • If the text field becomes empty again(the text is deleted) the submit button should be disabled again.

    I tried something like this:

    $(document).ready(function(){
        $('input[type="submit"]').attr('disabled','disabled');
        $('input[type="text"]').change(function(){
            if($(this).val != ''){
                $('input[type="submit"]').removeAttr('disabled');
            }
        });
    });
    

    ... but it doesn't work. Any ideas? Thanks.

    解决方案

    The problem is that the change event fires only when focus is moved away from the input (e.g. someone clicks off the input or tabs out of it). Try using keyup instead:

    $(document).ready(function() {
         $(':input[type="submit"]').prop('disabled', true);
         $('input[type="text"]').keyup(function() {
            if($(this).val() != '') {
               $(':input[type="submit"]').prop('disabled', false);
            }
         });
     });
    

    这篇关于jQuery禁用/启用提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆