【发布时间】:2018-06-28 19:39:39
【问题描述】:
我有一个验证页面上的文本框的 ajax 函数, 然后我需要对该值做一些事情(在数据库中搜索并在文本框中显示结果)。 然后,我需要另一个不同功能的验证功能。所以..
function validarRut(textBox) {
var resultado;
$.ajax({
type: "POST",
url: "AgendarInduccion.aspx/validarRut",
data: "{rut:" + JSON.stringify(textBox) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response){
alert("valido");
},
failure: function (response) {
alert("ERROR");
}
});
}
这是验证功能(有效!): 以及在数据库中搜索的功能:
function buscarEmpresa(textBox) {
var res;
res = validarRut(textBox.value); // I need the result, to works with this function.!
alert("VALOR" + res);
if (res) {
var resultado;
$.ajax({
type: "POST",
url: "/AgendarInduccion.aspx/buscarEmpresa",
data: "{RutEmpresa:" + JSON.stringify(textBox.value) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (response) {
resultado = response.d;
var nombre = resultado[0];
var razonSocial = resultado[1];
var direccion = resultado[2];
if (nombre == "nulo") {
alert("El Rut de la Empresa No se ha Encontrado en Nuestra Base de Datos, favor de ingresar!");
// crear empresa!
// la institucion no existe; se debe crear una!
//btGuardar.Visible = true;
//btCancelar.Visible = true;
//txtRutEmpresa.Enabled = false;
//txtNombreEmpresa.Enabled = true;
//txtRazonSocial.Enabled = true;
//txtDireccion.Enabled = true;
document.getElementById('<%= txtRutEmpresa.ClientID %>').disabled = true;
document.getElementById('<%= txtNombreEmpresa.ClientID %>').disabled = false;
document.getElementById('<%= txtRazonSocial.ClientID %>').disabled = false;
document.getElementById('<%= txtDireccion.ClientID %>').disabled = false;
$('#<%= txtRazonSocial.ClientID%>').val(razonSocial); //razon social desde SII
document.getElementById('<%=btGuardar.ClientID %>').style.display = 'inline';
document.getElementById('<%=btCancelar.ClientID %>').style.display = 'inline';
document.getElementById('<%=btActualizar.ClientID %>').style.display = 'none';
} else {
$('#<%= txtNombreEmpresa.ClientID%>').val(nombre);
$('#<%= txtRazonSocial.ClientID%>').val(razonSocial);
$('#<%= txtDireccion.ClientID%>').val(direccion);
}
},
failure: function () {
alert('No Realizado.');
}
});
}
}
我还有第三个使用“function validarRut(textBox)”的函数;所以我不想把搜索功能放在成功中,因为我需要重新使用该功能。有什么帮助吗? PD: async: false 不适合我。
【问题讨论】:
-
success: function(response){ buscarEmpresa(textBox) }, -
如果 async:false 不是一个选项,你为什么要使用它?
-
因为我在测试,但是是一个不好的解决方案;抱歉未注释
-
@mplungjan 如果我在 validarRut(textBox); 中使用它我不能重用与 buscarEmpresa(textBox) 不同的另一个函数
-
function validarRut(textBox,search) { var callSearch = search;和success: function(response){ if (search) buscarEmpresa(textBox) },- 现在您可以通过validarRut(textBox,true)搜索或通过validarRut(textBox)不搜索
标签: javascript jquery ajax callback promise