根据确认对话框提交复选框输入

时间:2013-12-26 10:35:35

标签: javascript php ajax

我有多行,每行都是一个带有复选框的表单。我试图根据有关行中映射的对象的id的一些数据来总结表单。

每个表单都是这样的:

<?php foreach ($tables as $table): ?>
   <tr>
   <td><?php echo $table->get_id(); ?></td>
       <form method="POST" action="/kviberg/admin/sparabord">
         <input type="hidden" name="table_id" value="<?php echo $table->get_id(); ?>">
         <td>
           <span style="display: none;">
             <?php if($table->get_available() == 1) echo "Yes"; else echo "No"; ?>
           </span>
           <input onclick="confirm_unavailable_table(this.form);" type="checkbox" name="available" value="<?php echo $table->get_available(); ?>" <?php if($table->get_available() == 1) echo "checked"; ?>>
         </td>
       </form>
<?php endforeach; ?>

功能:

function confirm_unavailable_table(form) {
   event.preventDefault();         <--- ERROR
   var id = form.table_id.value;
   $.ajax({
     type: "POST",
     cache: false,
     url: "/kviberg/views/ajax_check_availability.php", 
     data: {id : id},
     dataType: "json",
     success: function(data) {
        if(data.res_id > 0) {
          if(confirm("There are no reservations (reservations id: " + data.res_id + ").\nDo you want to continue?"));
            form.submit();
          }
        }
        else if(data.sub_id > 0) {
          if(confirm("There are subscriptions (prenumerations id: " + data.sub_id + ").\nDo you want to continue?")) {
            form.submit();
          }
        }
        else if(data.sub_id == 0 && data.res_id == 0) {
          form.submit();
        }
    },
    error: function(data) {
      console.log("error on data");
      console.log(data);
    }
  });
}

现在,表单永远不会通过ajax调用提交。是否与异步行为有关?

1 个答案:

答案 0 :(得分:0)

表单提交,它不会因event.preventDefault();而更改任何值 - 这会阻止复选框更改已检查状态。