选中复选框时提交表单 – 教程

我正在尝试实现类似于37signals的ta-da列表的效果 – 我希望我的用户能够通过选中“完成”框来检查列表中的项目 – 换句话说,表单被提交到服务器在检查框。 有没有人知道一个涵盖这样的东西的教程,或者可以指出我正确的方向?

谢谢Rob

如果我理解你的问题:

你可以使用jQuery和AJAX来完成这个任务。 在第一个例子中,我这样做而没有提交整个表单,只提交复选框的值:

jQuery("#myCheckbox").click(function() { var $checkbox = jQuery(this); var checkboxData = "checkboxvalue=" + $checkbox.val(); jQuery.ajax({ url: "http://some.url.here", type: "POST", data: checkboxData, cache: false, dataType: "json", success: function(data) { if(data["success"]) { //do some other stuff if you have to //this is based on the assumption that you're sending back //JSON data that has a success property defined } } }); }); 

据推测,你在服务器端有一些处理post的东西。

如果您确实想要提交表单,则可以执行与上面相同的操作,除非您要序列化表单数据:

 jQuery("#myCheckbox").click(function() { var formData = jQuery("#formID").serialize(); jQuery.ajax({ url: "http://some.url.here", type: "POST", data: formData, cache: false, dataType: "json", success: function(data) { if(data["success"]) { //do some other stuff if you have to //this is based on the assumption that you're sending back //JSON data that has a success property defined } } }); }); 

这很简单…

  


当您单击复选框时,上面将提交表单…如果那就是您想要的idk。
所以它会做默认的表单提交过程……