作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
希望这是基本的 JavaScript,并且答案很简单,因为我是新手。我只想确保在用户可以继续之前所有必填字段都不为空。在我的 <form>
action
=
payment.php
并且提交按钮包含 onclick="insert();"
当 child_name 留空时,它只是继续到下一页而不是提醒。这是代码:
<script type="text/javascript">
function insert() {
if (window.XMLHttpRequest) {
xmlhttp = new XLMHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
cn1 = 'child_name'+document.getElementById('child_name').value;
ag2 = 'age'+document.getElementById('age').value;
hm3 = 'hometown'+document.getElementById('hometown').value;
bg4 = 'boy_girl'+document.getElementById('boy_girl').value;
fn5 = 'first_name'+document.getElementById('first_name').value;
ln6 = 'last_name'+document.getElementById('last_name').value;
email = 'email'+document.getElementById('email').value;
ad8 = 'address1'+document.getElementById('address1').value;
ad9 = 'address2'+document.getElementById('address2').value;
ct10 = 'city'+document.getElementById('city').value;
st11 = 'state'+document.getElementById('state').value;
zp12 = 'zip'+document.getElementById('zip').value;
if (cn1.equals("")) {
xmlhttp.open('POST', 'payment.php', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(cn1&ag2&hm3&bg4&fn5&ln6&email$ad8&ad9&ct10&st11&zp12);
} else {
alert ("Please enter all required fields.");
}
}
</script>
最佳答案
这里有几个问题:
cn1 = 'child_name'+document.getElementById('child_name').value;
在这种情况下,您将 cn1 分配给字符串 'child_name'
+ child_name 文本框的值...它永远不会等于空字符串。
其次,您需要 insert()
方法在出错时返回 false
以停止按钮的预期操作。
关于javascript - 我想在 JavaScript 中验证我的必填字段不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975734/
我是一名优秀的程序员,十分优秀!