希望这是基本的 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
发生错误以停止按钮的预期操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)