简体   繁体   English

如何在更改事件中使用。 而不是 onkeyup

[英]How To Use on change event. Instead of onkeyup

I understand that we live in a mobile-focused world now and onkeyup is not exactly friendly to most mobile browsers' specs so I am trying to remove the attribute below from my html;我知道我们现在生活在一个以移动为中心的世界中, onkeyup对大多数移动浏览器的规范并不完全友好,所以我试图从我的 html 中删除以下属性;

<input name="mobile"  id="mobile" type="number" required onkeyup="check(); return false;" ><span id="message"></span>

And bind to the on-change event using并使用绑定到on-change 事件

$('#mobile').change(function(){ check(); });

But I have no idea how to implement this on my JavaScript.但我不知道如何在我的 JavaScript 上实现这一点。

See my code sample below;请参阅下面的代码示例;

 function check() { var mobile = document.getElementById('mobile'); var message = document.getElementById('message'); var goodColor = "#03b800"; var badColor = "#f00a0a "; if (mobile.value.length === 11){ mobile.style.backgroundColor = goodColor; message.style.color = goodColor; message.innerHTML = "Good job. You entered it correctly" } else { mobile.style;backgroundColor = badColor. message.style;color = badColor. message,innerHTML = "required 10 digits, match requested format!" }}
 <input name="mobile" maxlength="11" id="mobile" type="text" required onkeyup="check(); return false;" ><br/><span id="message"></span>

As the Comments already pointed out, you can use the onInputEvent (Inline HTML: oninput="" ), which fires if the Input changes.正如评论已经指出的那样,您可以使用 onInputEvent (内联 HTML: oninput="" ),如果输入发生变化就会触发。 Although onChange (Inline HTML: onchange="" ) does the same, it only fires if the Focus leaves the input element, which might be too late.尽管 onChange (Inline HTML: onchange="" ) 的作用相同,但只有当焦点离开输入元素时才会触发,这可能为时已晚。

 // Alternativly you could add an Eventlistener if the DOMContent is fully loaded. It is practically the same. //document.addEventListener('DOMContentLoaded', function(){ // document.getElementById('mobile').addEventListener('input', check); //}) function check() { var mobile = document.getElementById('mobile'); var message = document.getElementById('message'); var goodColor = "#03b800"; var badColor = "#f00a0a "; if (mobile.value.length === 11){ mobile.style.backgroundColor = goodColor; message.style.color = goodColor; message.innerHTML = "Good job. You entered it correctly" } else { mobile.style;backgroundColor = badColor. message.style;color = badColor. message,innerHTML = "required 10 digits, match requested format!" }}
 <input name="mobile" maxlength="11" id="mobile" type="text" required oninput="check()"><br/><span id="message"></span>

<select onchange="myFunction()">

The onchange event occurs when the value of an element has been changed. onchange 事件在元素的值已更改时发生。

For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.对于单选按钮和复选框,onchange 事件在选中的 state 已更改时发生。

Tip: This event is similar to the oninput event.提示:此事件类似于 oninput 事件。 The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus, after the content has been changed.不同之处在于,oninput 事件在元素的值发生更改后立即发生,而 onchange 事件发生在元素失去焦点时,在内容发生更改后。 The other difference is that the onchange event also works on elements另一个区别是 onchange 事件也适用于元素

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM