问题描述
问题出现的环境背景及自己尝试过哪些方法
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
function inp4(){
if(validateIdCard($('input[name="idcard"]').val(),'#idcard_tip')){
$.ajax({
url: "${basePath}/realNameUtils/checkUniqueIdCardNum?idCardNum=" + $('input[name="idcard"]').val() + "&type=" + "natural",
type: "post",
async: false,
dataType: "json",
success: function (data) {
if (!data) {
$("#idcard_tip").html("身份证已被认证!");
$("#idcard_tip").show();
return false;
}else{
$("#idcard_tip").hide()
return true;
}
}
});
}else{
$("#idcard_tip").show()
return false;
}
}
改成callback
function inp4(callback){
if(validateIdCard($('input[name="idcard"]').val(),'#idcard_tip')){
$.ajax({
url: "${basePath}/realNameUtils/checkUniqueIdCardNum?idCardNum=" + $('input[name="idcard"]').val() + "&type=" + "natural",
type: "post",
async: false,
dataType: "json",
success: function (data) {
if (!data) {
$("#idcard_tip").html("身份证已被认证!");
$("#idcard_tip").show();
callback(false)
}else{
$("#idcard_tip").hide()
callback(true)
}
}
});
}else{
$("#idcard_tip").show()
return false
}
}
this.inp4(function(res){
console.log(res)
})
控制台没有打印值 不起作用
使用callback 下面是可以的了
function inp4(callback){
if (creditcode($('input[name="enterpriseIdCard"]').val(), '#creditcode_tip')) {
$.ajax({
url: "${basePath}/realNameUtils/checkUniqueIdCardNum?idCardNum=" + $('input[name="enterpriseIdCard"]').val() + "&type=" + "creditCode",
type: "post",
async: false,
dataType: "json",
success: function (data) {
if (!data) {
$("#creditcode_tip").html("证件号码已被占用!");
$("#creditcode_tip").show();
typeof callback === 'function' && callback(false)
}else{
$("#creditcode_tip").hide()
typeof callback === 'function' && callback(true)
}
}
});
} else {
$("#creditcode_tip").show()
typeof callback === 'function' && callback(false)
}
}
inp4(function(res){
console.log(res)
})
如果不if里面不用ajax请求的话 直接返回一个true 调用this.inp4()是能获取的true 用ajax就不好使了 不知道为什么 求教
你这种回调拿到的应该是undefind,用callback回调