[英]Ajax call doesn't work within click() in Javascript
I'm trying to update my page dynamically using ajax and php. 我正在尝试使用ajax和php动态更新我的页面。 The ajax call works fine until I put it within a click() function.
ajax调用工作正常,直到我把它放在click()函数中。 The alert works ok
警报工作正常
$(document).ready(function(){
$("#formbutton").click(function(){
alert("test");
$.ajax({
url: 'url.php',
type: 'GET',
dataType: 'html',
success: function (data) {
$("#response").html(data);
}
});
});
});
This works fine in debugging: 这在调试中工作正常:
$(document).ready(function(){
alert("test");
$.ajax({
url: 'url.php',
type: 'GET',
dataType: 'html',
success: function (data) {
$("#response").html(data);
}
});
});
What am I doing wrong? 我究竟做错了什么? Thanks
谢谢
Is the button inside a FORM tag? FORM标签内的按钮是?
If that is the case, take into account that the default behavior of a button is to submit the form, probably you don't see the AJAX response because the default event handler of the submit form is executed before the AJAX success handler. 如果是这种情况,请考虑按钮的默认行为是提交表单,可能您没有看到AJAX响应,因为提交表单的默认事件处理程序在AJAX成功处理程序之前执行。
Try to: 尝试:
Remove the FORM if you don't need it. 如果您不需要,请删除FORM。
If you want to keep the form, use a "preventDefault": 如果要保留表单,请使用“preventDefault”:
$('#formbutton').click(function(evt) { evt.preventDefault(); /* ... */ });
If you are using a button
tag, you can also change the type to "button": <button type="button">
, so you ensure that the browser is not using type=submit
as default. 如果使用
button
标记,还可以将类型更改为“按钮”: <button type="button">
,这样可以确保浏览器不使用type=submit
作为默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.