在我解决与jquery有关的所有事情的过程中,又是一次羞辱性的斗争:
我正在尝试将表单发布到php页面create.php
点击一个按钮(惊喜)我将一个表单序列化并发布到php页面。像这样:
$('#createbutton').click(function (event) {
event.preventDefault();
$.post("create.php", $("#creation").serialize());
});
以下是表格:
<form method="post" id="creation">
Type:<select name="table">
<option value="Standard">Standard</option>
<option value="Special">Special</option>
<option value="Writing">Writing</option>
</select></br></br>
<select name="department">
<option value="CSC">CSC</option>
<option value="MAT">MAT</option>
</select>
<input value="100" size="3" type="text" name="CRN" maxlength="3"/></br>
Course Title:</br> <input size="32" type="text" name="title" maxlength="32" placeholder="Enter a title"/></br>
Date received by UCF: </br><input name="addDate" id="datepicker" type="text" placeholder="Click here to add date"></br>
LEP: <input type="checkbox" name="lep" value="1" /></br>
</br></br>
<button id="createbutton" class="ui-state-default ui-corner-all">Create</button>
</form>
它都不起作用,甚至不是event.preventDefault();
。看起来很简单,我只是不知道我错过了什么。如果有人能发现错误,它会让我进入睡眠状态。我很感激。感谢。
答案 0 :(得分:1)
我通常在事件函数中返回false以防止默认操作,但是你应该也可以使用它。
你需要做一些调查,看看它为什么不起作用。 console.log是与Google Chrome浏览器Developer tools一起进行调试的绝佳工具。
要调试这个,我首先要将功能更改为:
$('#createbutton').click(function (event) {
console.log($("#creation").serialize()));
return false;
});
您可以查看序列化表单数据是否符合您的预期。
下一步是查看ajax调用发送的传出数据。您可以在Chrome的开发人员工具的“网络”标签中执行此操作。
答案 1 :(得分:0)
我想我只需将事件包装在一个匿名函数中:
$(function (){
$('#createbutton').click(function () {
$.post('create.php', $('#creation').serialize());
});
});