[英]Can't validate checkbox value with jquery /ajax
在我的html表單中,除復選框值外,所有數據均已成功驗證。 無法通過jquery / ajax請求進行驗證。
這是我的html表單ajax和html代碼(僅術語部分):
<tr>
<td>Terms & Conditions</td>
<td><input type="checkbox" name="terms"/> I agree to your <a href="">terms and
conditions</a>.</td>
<script>
$('#form1').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'regProcess.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#info1').html('');
$.each( data, function( key, value ) {
$('#info1').append('<p>'+value+'</p>');
});
}
});
});
</script>
在regProcess.php頁面中,以下是我的Checkbox驗證代碼,但未驗證。
if(isset($_POST['terms']) && $_POST['terms'] == ""){
$msg[] = "You must agree our terms and conditions";
$msg1['error'] = true;
}
給復選框一個ID
<tr>
<td>Terms & Conditions</td>
<td><input type="checkbox" id="terms" name="terms"/> I agree to your <a href="">terms and
conditions</a>.</td>
然后使用它來找出復選框是否已選中??
<script>
$('#form1').submit(function(event) {
event.preventDefault();
if($('#terms').is(':checked') )
{
$.ajax({
type: 'POST',
url: 'regProcess.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#info1').html('');
$.each( data, function( key, value ) {
$('#info1').append('<p>'+value+'</p>');
});
}
});
}
else{
alert("Check terms and condition");
}
});
</script>
如果未選中$_POST['terms']
則不會設置該復選框。 相反,它應該是:
if (empty($_POST['terms']) || $_POST['terms'] !== 'on') {
要不就
if (empty($_POST['terms'])) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.