我想创建一个带有文本框的表单并使用JavaScript提交按钮。只有当我在文本框中输入总共10位数的电话号码时,该按钮才会启用。
答案 0 :(得分:1)
非常简单的解决方案:https://jsfiddle.net/tkx3dcna/
HTML
<input id="phone" type="text">
<button id="btn" disabled>Send</button>
JS
var btn=document.getElementById("btn");
var phone=document.getElementById("phone");
phone.onkeyup=function(){
btn.disabled=!(phone.value.length===10);
};
答案 1 :(得分:0)
你走了,虽然马达拉是对的,你应该自己试试这些东西,不是吗?
post
&#13;
var num = document.querySelector("input[type='number']"),
submit = document.querySelector("input[type='submit']");
num.onkeyup = function(event) {
var val = this.value + '';
if (val.length === 10) {
submit.disabled = false;
} else {
submit.disabled = true;
}
};
// Use below method instead, submit button shows immediately after the numbers are a length of 10
/*
function areYouReady() {
var val = num.value + '';
if (val.length === 10) {
isReady = true;
submit.disabled = false;
} else {
isReady = true;
submit.disabled = true;
}
requestAnimationFrame( areYouReady );
}
areYouReady();
*/
&#13;