[英]Input Validation onblur focus
无法正常工作
<script type="text/javascript">
//<![CDATA[
<!--
function checkPrice(form) {
var input=0;
input=document.stickerPrice.value;
if (input<100000) {
alert("Sticker price must be more than $100,000");
document.getElementsByName("stickerPrice").focus();
}
}
// -->
//]]>
</script>
这是标签部分。
<form action=''>
<p>Sticker Price on the Car (in dollars):
<input type="text" name="stickerPrice" size="15" onblur="checkPrice(this.formData)" /></p>
</form>
如果 onblur 事件小于 10,000,我希望出现一个警告框,然后将焦点放回到该文本框上。
您可以通过调用传递输入本身
onblur="checkPrice(this.formData)"
然后你可以通过调用直接检查它的值
form.value
并专注于
form.focus();
这是一个工作示例。
function checkPrice(form) { var input=0; input=form.value; if (input<100000) { alert("Sticker price must be more than $100,000"); form.focus(); } }
<form action=''> <p>Sticker Price on the Car (in dollars): <input type="text" name="stickerPrice" size="15" onblur="checkPrice(this);" /></p> </form>
我建议您解决并使用 onblur 删除 focus()。
function checkPrice(form) { var input=0; input=form.value; if (input<100000) { alert("Sticker price must be more than $100,000"); form.value=''; } }
<form action=''> <p>Sticker Price on the Car (in dollars): <input type="text" name="stickerPrice" size="15" onblur="checkPrice(this);" /></p> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.