Jas*_*son 2 javascript php jquery
我有一个连接到PayPal api的脚本来检查有效的信用卡输入.该脚本可能需要大约5秒钟才能执行,并且为了防止用户多次点击"提交",如果他们没有看到立即响应,我想放置一个"请稍候"指示符.
我有一个div,"pleaseWait"是隐藏的.在jQuery我有:
$('#submit').click(function(){
$('#pleaseWait').show();
});
Run Code Online (Sandbox Code Playgroud)
唯一的问题是如果有问题,它将发回php错误,"请等待"将继续在屏幕上.我决定尝试另一种方法,并在脚本开始运行后在php中回显jQuery,并在出现错误时隐藏它.
/* Echo the jQuery so the "Please wait" indicator will show on screen */
echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\"></script>";
echo "<script type='text/javascript' charset='utf-8'>";
echo "\$(document).ready(function(){";
echo "\$('#pleaseWait').show();";
echo "});";
echo "</script>";
if($error == ""){
/* There is not an error, run php. */
}else{
/* There was an error, stop the jQuery from displaying the "please wait" and display the error */
echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\"></script>";
echo "<script type='text/javascript' charset='utf-8'>";
echo "\$(document).ready(function(){";
echo "\$('#pleaseWait').hide();";
echo "});";
echo "</script>";
}
Run Code Online (Sandbox Code Playgroud)
这可以工作,但似乎非常混乱.除了PHP中的多个回声之外,还有更好的方法吗?
尝试使用ajax
$('#submit').click(function(){
$('#pleaseWait').show();
$.post('url',card,function(data){
$('#pleaseWait').hide();
alert(data);
})
});
Run Code Online (Sandbox Code Playgroud)