繁体   English   中英

无法使用jquery / ajax验证复选框的值

[英]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"/>&nbsp; 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"/>&nbsp; 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM