将复选框值提交给PHP而不提交按钮

时间:2013-08-09 10:51:29

标签: php jquery

我的表单只有一个复选框:

<form action="escreve.php" method="post">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave">Autosave</label>
    <input type="submit" name="formSubmit" value="Submit" />
</form>

这将触发escreve.php。它工作正常,但问题是如何在没有提交按钮的情况下触发escreve.php?在这种情况下似乎没有意义,因为这个表单只有一个输入(复选框)。

我想让escreve.php在用户点击或按下时读取复选框的值。既然我也在使用jQuery,那可能吗?

感谢您的帮助!

6 个答案:

答案 0 :(得分:2)

你可以使用一些javascript来触发提交。

<form id='form' action="escreve.php" method="post">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave">Autosave</label>
</form>

<script>
$(document.ready(function (e) {
     $("input#autosave").click(function (e) {
         $("#form").submit();
     });
});
</script>

答案 1 :(得分:1)

使用jquery form submit method

试试这个

<form action="escreve.php" method="post" id="myForm">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave" id="autosave">Autosave</label>
    <input type="submit" name="formSubmit" value="Submit" />
</form>

$(document).ready(function(){
  $("#autosave").click(function(){
      $("#myForm").submit()
  });
});

答案 2 :(得分:1)

不要摆脱按钮,摆脱复选框。

<form action="escreve.php" method="post">
    <input type="submit" name="autosave" value="Autosave" />
</form>

答案 3 :(得分:0)

确定你可以。请参阅jQuery onChange()或onClick()并使用带有.submit()的jQuery提交表单

答案 4 :(得分:0)

   var form = $('form');  

  $('[name="autosave"]').click(function() {
        $.ajax({
          type: form.attr('method'),
          url: form.attr('action'),
          data: form.serialize()
        }).done(function() {
          // Optionally alert the user of success here...
        }).fail(function() {
          // Optionally alert the user of an error here...
        });
        event.preventDefault(); // Prevent the form from submitting via the browser.
    });

答案 5 :(得分:0)

  • 为表单提供一些ID
  • 然后在复选框
  • 的更改事件上使用submit()方法提交表单

e.g

$('#chkid').change(function() { $('#frmid').submit(); });