Javascript Jquery没有';t阻止提交按钮操作
你能帮我做这些吗 我正在进行一个PHP Jquery AJAX JSON搜索,其想法是使用一个PHP表单,在其中键入特定员工的ID,然后通过AJAX在div中以相同的PHP表单显示姓名 我的问题是,我可以显示我想在div中显示的消息,因为当我按下submit按钮时,它总是重定向到表单中指定的操作页面,而不只是将消息显示到div中,所以您能告诉我我有什么问题吗?正如您将看到的,我在代码中指定了e.preventDefault(),如下所示:Javascript Jquery没有';t阻止提交按钮操作,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,你能帮我做这些吗 我正在进行一个PHP Jquery AJAX JSON搜索,其想法是使用一个PHP表单,在其中键入特定员工的ID,然后通过AJAX在div中以相同的PHP表单显示姓名 我的问题是,我可以显示我想在div中显示的消息,因为当我按下submit按钮时,它总是重定向到表单中指定的操作页面,而不只是将消息显示到div中,所以您能告诉我我有什么问题吗?正如您将看到的,我在代码中指定了e.preventDefault(),如下所示: $(document).ready(function()
$(document).ready(function() {
$("#submit_id").click(function (e) {
e.preventDefault();
var id = $("input#ID_id");
if(validaForm(id)){
$("#submit_id").hide();
var url = $("#form_id").attr('action');
var data = $("#form_id").serialize();
var type = $("#form_id").attr('method');
$("#LoadingImage").show();
$("#ajax_id").html("<div class='cargando'> realizando busqueda</div>");
$.ajax({
url:url,
data:data,
type:type,
cache: false,
contentType: "application/x-www-form-urlencoded",
dataType: 'json',
encode: true
.done(function(data) { // using the done promise callback
if ( ! data.success) {
$("#LoadingImage").fadeOut();
if (data.errors.ID_name) {
$("#ajax_id").html("<div class='cargando'> Debe especificar el ID</div>");
} // if
} // if
else {
$("#ajax_id").html("<div class='cargando'> Tudo Ben</div>");
} // else
}) // done-promise
.fail(function(data) { // using the fail promise callback
console.log(data);
}); // fail-promise
}); // AJAX call
} // validaForm*/
});
});
function validaForm(id){
var id_val = id.val().trim();
if((id_val=="") || id_val.replace(/s+/,'') == ''){
alert("Favor ingrese el ID");
id.addClass("posicionamiento");
$("#div_id").html("<div class='error'>Debe especificar el nombre</div>");
return false;
}else{
id.removeClass("posicionamiento");
$("#div_id").empty();
}
return true;
}
$(文档).ready(函数(){
$(“提交id”)。单击(功能(e){
e、 预防默认值();
变量id=$(“输入id”);
if(validaForm(id)){
$(“提交id”).hide();
var url=$(“#form#id”).attr('action');
var data=$(“#form_id”).serialize();
var type=$(“#form#id”).attr('method');
$(“#加载图像”).show();
$(“#ajax_id”).html(“realizando busqueda”);
$.ajax({
url:url,
数据:数据,
类型:类型,
cache:false,
contentType:“application/x-www-form-urlencoded”,
数据类型:“json”,
编码:正确
.done(函数(数据){//使用done承诺回调
如果(!data.success){
$(“#加载图像”).fadeOut();
if(data.errors.ID\u name){
$(“#ajax_id”).html(“Debe specifical el id”);
}//如果
}//如果
否则{
$(“#ajax_id”).html(“Tudo-Ben”);
}//否则
})//我答应了
.fail(函数(数据){//使用失败承诺回调
控制台日志(数据);
})不履行诺言
});//AJAX调用
}//有效形式*/
});
});
函数有效形式(id){
变量id_val=id.val().trim();
如果((id_val==”)| id_val.replace(/s+/,“”)==“”){
警报(“赞成进入el ID”);
id.addClass(“posicionamiento”);
$(“#div_id”).html(“Debe specifical el nombre”);
返回false;
}否则{
id.removeClass(“posicionamiento”);
$(“#div_id”).empty();
}
返回true;
}
HTML:
布斯卡多
巴斯卡多阿辛克罗诺酒店
我要一辆公共汽车:
您试图阻止的是单击事件,而不是提交本身
使用.submit
()而不是。单击事件处理程序中的(并将事件绑定到表单而不是submit按钮):
如果要在特定点提交表单,可以使用then$(“#form_id”).submit()代码>
更新:我做了一次调查,以找出问题所在:
您关闭了.ajax()
太晚了:-)
将此更改为:
$.ajax({
// ...
}) // AJAX call
.done(function(data) { // using the done promise callback
// ...
}) // done-promise
.fail(function(data) { // using the fail promise callback
// ...
}); // fail-promise
它已在小提琴中更正。尝试在$(“#提交_id”)中返回FALSE。单击函数或在HTML表单中使用“按钮”而不是“提交”组件。谢谢,我刚试过,但仍然将我发送到指定的操作php页面。您能给我们看一下HTML表单吗?另外,我非常确定done
和fail
的位置不正确?为什么它们在你传递给$的对象中?ajax
?Darian,这是html代码(很抱歉,我不知道如何正确地发布它)BUSCADOR BUSCADOR AsincronoID a buscar:@PabloTobar这个解决方案有什么错误?
$(document).ready(function() {
$("#form_id").submit(function (e) {
e.preventDefault();
// ...
$.ajax({
// ...
.done(function(data) { // using the done promise callback
// ...
}) // done-promise
.fail(function(data) { // using the fail promise callback
// ...
}); // fail-promise
}); // AJAX call
$.ajax({
// ...
}) // AJAX call
.done(function(data) { // using the done promise callback
// ...
}) // done-promise
.fail(function(data) { // using the fail promise callback
// ...
}); // fail-promise