我是一名AJAX菜鸟。我正在编写代码来理解它,但无论我怎么做都无法工作。当用户按下“保存”按钮时,代码中的Textarea应更新comment_area
的{{1}}注释。我的AJAX代码可能有一个错误,我找不到。
我的AJAX脚本:
id=218
<script type="text/javascript">
$(document).ready(function() {
$("#save").submit(function() {
var text = $('#breaking_news_text').val();
var id = 218,
$.ajax({
type: "POST",
url: "update.php",
data: {comment_area:text , id:id}
success: function() {
alert("sucess");
}
});
});
});
</script>
我的update.php文件
<div id="b_news">
<form method="post" action="">
<div>
<div>
<textarea id="breaking_news_text" class="breaking_news_text" rows="6" cols="50" placeholder="Add text here..." required></textarea>
</div>
</div>
<div>
<input type="button" id="save" value="Save Changes"/>
</div>
</form>
</div>
答案 0 :(得分:2)
submit
适用于form
,您可以在input
元素上使用。
尝试:
$("#b_news form").submit(function(evt) {
evt.preventDefault(); //this is required to stop the default form submission
文档可以是found here
此外,如果动态加载这些dom元素,您可能需要read up on event delegation