三生石@ 2009-10-20 14:21 采纳率: 100%
浏览 426
已采纳

禁用 / 启用提交按钮

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.

转载于:https://stackoverflow.com/questions/1594952/jquery-disable-enable-submit-button

  • 写回答

17条回答 默认 最新

  • from.. 2009-10-20 14:26
    关注

    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);
            }
         });
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?