I have this code:
我有这个代码:
<script type="text/javascript">
$(document).ready(function() {
$('#submit').prop('disabled', true);
$('#links').change(function() {
$('#submit').prop('disabled', this.value == "" ? true : false);
})
});
But when the first thing I do when I load the page is rightclick paste and hit the button, the button is disabled.. I need to click 'outside' the text field before I click the button if I want it to work.. is there any way round this? Thanks in advance!
但是当我加载页面时我做的第一件事是右键单击粘贴并按下按钮,按钮被禁用..我需要单击“外部”文本字段,然后单击按钮,如果我希望它工作..是这有什么办法吗?提前致谢!
2 个解决方案
#1
0
You can provide a space-delimited list of events using on
, which will listen for any of the events related to the action that you're looking for:
您可以使用on提供以空格分隔的事件列表,该列表将侦听与您正在查找的操作相关的任何事件:
$('#links').on("change keyup paste", function() {
$('#submit').prop('disabled', this.value == "" ? true : false);
});
#2
0
The change()
event is a bit misleading as it waits until focus is lost until it actually triggers the function :
change()事件有点误导,因为它一直等到焦点丢失,直到它实际触发函数:
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
对于选择框,复选框和单选按钮,当用户使用鼠标进行选择时会立即触发事件,但对于其他元素类型,事件将延迟,直到元素失去焦点。
If you want to trigger it immediately, then you may want to consider binding to an event that does trigger immediately such as detecting input via the input
event :
如果要立即触发它,那么您可能需要考虑绑定到立即触发的事件,例如通过输入事件检测输入:
// 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);
});
Example
<!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>
#1
0
You can provide a space-delimited list of events using on
, which will listen for any of the events related to the action that you're looking for:
您可以使用on提供以空格分隔的事件列表,该列表将侦听与您正在查找的操作相关的任何事件:
$('#links').on("change keyup paste", function() {
$('#submit').prop('disabled', this.value == "" ? true : false);
});
#2
0
The change()
event is a bit misleading as it waits until focus is lost until it actually triggers the function :
change()事件有点误导,因为它一直等到焦点丢失,直到它实际触发函数:
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
对于选择框,复选框和单选按钮,当用户使用鼠标进行选择时会立即触发事件,但对于其他元素类型,事件将延迟,直到元素失去焦点。
If you want to trigger it immediately, then you may want to consider binding to an event that does trigger immediately such as detecting input via the input
event :
如果要立即触发它,那么您可能需要考虑绑定到立即触发的事件,例如通过输入事件检测输入:
// 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);
});
Example
<!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>