希望這是基本的JavaScript,答案很簡單,因爲我是新手。我只是想確保所有必填字段在用戶可以繼續之前不是空白的。在我的<form>
action
=
payment.php
和提交按鈕包含onclick="insert();"
當child_name留空時,它只進行到下一頁而不是提示。下面是代碼:我想用JavaScript驗證我的必填字段是否爲空
<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>
你的意思是 「JavaScript的」,而不是 「Java」 的,對不對? – Pointy
我的理解是,你希望我們糾正你的代碼,對吧? –
二進制'&'運算符不連接字符串。另外,你忘記了'='s和'encodeURIComponent'你的值。 – Bergi