【问题标题】:Button and Text input in JSJS中的按钮和文本输入
【发布时间】:2016-05-17 15:03:22
【问题描述】:

这是在我的 PHP 文件中:

<input style="text-align: center; max-width: 500px; margin-top: 0px;" type="text" placeholder="Paste your links here separated by a space" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Paste your links here separated by a space'" name='links' id='links' />
<!--lilbanner-->
<BR>
<button class="button alt" id="submit" type="submit">
    <?php printf($obj->lang['sbdown']); ?>
</button>
&nbsp;&nbsp;&nbsp;

我希望当文本输入为空时按钮被禁用,当用户输入文本并删除它时,它将再次为空,意味着按钮将再次被禁用。我有以下代码,它与我的其他网站完全一样,但由于某种原因它不起作用(空文本输入 = 按钮..)

<script type="text/javascript">
    $(document).ready(function() {
        $('.submit').prop('disabled', true);
        $('#link').change(function() {
            $('.submit').prop('disabled', this.value == "" ? true : false);
        })
    });
</script>

你能帮帮我吗?提前致谢!

【问题讨论】:

  • 我没有替换我知道的任何东西,但它仍然不起作用
  • 最简单的发现可能是解决方案:您的输入具有 id links 而不是您用于更改事件处理程序的 link
  • Aaaaand:您的按钮具有 id submit 而不是您在更改 disabled 属性时使用的 class submit
  • 我知道我没有用我的 IDS 和其他东西替换代码,但它仍然不起作用..
  • 我不明白...您没有根据您的 HTML 更改代码,然后您对它不起作用感到惊讶?如果您将#link 更改为#links 并将.submit 更改为#submit,它应该可以工作。检查jsbin.com/taqayirixa

标签: javascript button text input


【解决方案1】:

给你。你想要'keyup'和'paste'而不是'change':

$(document).ready(function() {
    $('#submit').prop('disabled', true);
    $('#links').bind('keyup paste', function() {
        $('#submit').prop('disabled', this.value == "" ? true : false);
    })
});

【讨论】:

    【解决方案2】:

    您的选择器错误,请查看修复:

      <script type="text/javascript">
        $(document).ready(function() {
          $('#submit').prop('disabled', true);
          $('#links').change(function() {
            $('#submit').prop('disabled', this.value == "" ? true : false);
          })
        });
      </script>
    

    Plunker:https://plnkr.co/edit/ETJiPg6BKDNkqbGHXCfk?p=preview

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多