【问题标题】:Make right click ---> Paste work in input style=text and button右键单击--->粘贴输入样式=文本和按钮
【发布时间】:2016-05-18 01:40:48
【问题描述】:

我有这个代码:

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

但是当我加载页面时做的第一件事是右键单击粘贴并点击按钮时,该按钮被禁用。如果我希望它工作,我需要在单击按钮之前单击文本字段的“外部” ..有什么办法解决这个问题吗?提前致谢!

【问题讨论】:

  • 将事件更改为input而不是change,如果您不希望每次用户键入时都触发它,但您也可以将其更改为paste,当发生某些事情时触发从剪贴板粘贴

标签: javascript jquery forms button


【解决方案1】:

您可以使用on 提供以空格分隔的事件列表,该列表将侦听与您要查找的操作相关的任何事件:

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

【讨论】:

    【解决方案2】:

    change() 事件有点误导,因为它一直等到失去焦点才真正触发函数:

    对于选择框、复选框和单选按钮,触发事件 当用户用鼠标进行选择时立即,但 其他元素类型事件被推迟到元素丢失 重点。

    如果您想立即触发它,那么您可能需要考虑绑定到立即触发的事件,例如通过input 事件检测输入:

    // The input event should handle any types of input that comes in (including paste)
    $('#links').on('input', function(){
        $('#submit').prop('disabled', this.value == "" ? true : false);
    });
    

    示例

    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>Copy Example</title>
    </head>
    
    <body>
      <input id='links' />
      <input id='submit' type='submit' />
    
      <script type="text/javascript">
        $(function() {
          $('#submit').prop('disabled', true);
          $('#links').on('input', function() {
            console.log('Input detected');
            $('#submit').prop('disabled', this.value == "" ? true : false);
          });
        });
      </script>
    </body>
    
    </html>

    【讨论】:

    • 我已经更新它以使用不同的事件,即 inputpropertychange,它们应该可以处理这两种情况(按键事件和粘贴)。
    • 非常感谢!就像我想要的那样工作! :))))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多