1、添加async:false.即修改为同步了,什么意思?(按同事解释就是,这是等这个ajax有了返回值后才会执行下面的js。一语道破天机,怪不得以前很多ajax调用里面的赋值都不起作用)。这样等ajax给bol赋值完毕后,才执行下面的js部分。而刚刚异步的话,还没有来得及赋值,就已经return了。
$(‘document‘).ready(function() { function newajax(info) { $.ajax({ type: ‘get‘, async: false, url: ‘user.php‘, data: info, success: function(msg) { if (msg == 1) { rs = 1; } else if (msg == 0) { rs = 0; } else { rs = -1; } } }); return rs; } $(‘#edit_signature‘).bind(‘click‘, function() { $(‘#s1‘).html("<input type=‘text‘ placeholder=‘输入签名‘ class=‘signature‘/>"); $(‘.signature‘).blur(function() { var signature = $(‘.signature‘).val(); if (signature == ‘‘) { $(‘.signature‘).hide(); } else { var info = ‘act=signature&value=‘ + signature; var msg1 = newajax(info); if (msg1 == 1) { showmsg(‘修改成功‘); $(‘.signature‘).hide(); $(‘#signature‘).html(signature); } else if (msg1 == 0) { showmsg(‘修改失败‘); $(‘.signature‘).hide(); } else { showmsg(‘请登录‘); location.href = "http://localhost/bookboss/member/login.php"; } } }); }); $(‘#btn-save‘).click(function() { var name1 = $(‘#name‘).val(); name = encodeURIComponent(name1, ‘UTF-8‘); var school = $(‘#school‘).val(); school = encodeURIComponent(school, ‘UTF-8‘); var professional = $(‘#professional‘).val(); professional = encodeURIComponent(professional, ‘UTF-8‘); var info = ‘act=edit&name=‘ + name + ‘&school=‘ + school + ‘&professional=‘ + professional; var msg1 = newajax(info); if (msg1 == 1) { showmsg(‘修改成功‘); $(‘#name1‘).html(name1); } else if (msg1 == 0) { showmsg(‘修改失败‘); } else { showmsg(‘请登录‘); location.href = "http://localhost/bookboss/member/login.php"; } }); });
时间: 2024-10-09 04:57:46