我使用Codeigniter MVC框架和im掙扎與jquery Ajax。 我的問題是你如何在ajax成功函數中添加條件語句?ajax成功函數中的條件語句
基本上就是我想要做的就是在成功進入會重定向到另一個頁面,如果不將顯示一個錯誤:手機號碼不相符,或者如果空請填寫空白。
控制器方法
if($mobile_number == "") {
$data = array('message' => "Please fill up the blanks", 'success' => 'no');
}
elseif($mobile_number != "123") {
$data = array('message' => "Please does not match", 'success' => 'no');
}else {
#redirect('home/test');
$data = array('message' => "Mobile match.", 'success' => 'yes');
}
$output = json_encode($data);
echo $output;
my_ajax.js
$(document).ready(function(){
$("#request_submit").click(
function(){
var mobtel=$("#mobtel").val();
$.ajax({
type: "POST",
url: "post_action",
dataType: "json",
data: "mob_tel="+mobtel,
cache:false,
success: function (data) {
if(data.success == 'yes') {
$("#form_message").html(data.message).fadeIn('slow');
}
else if(data.success == 'no') {
alert('error');
}
}
});
});
});
預先感謝。希望有人能幫助我。 (T_T)
我不認爲你應該修剪'data',因爲它已經是一個解析對象而不是一個字符串。 – MMM 2012-01-30 11:44:18
而且你的第二個'在你成功的功能if'的說法應該是和其他'聲明if'而是因爲'data.success'不能同時'「yes''和'」 no'' – MMM 2012-01-30 11:51:06
更新我的代碼。仍然不起作用。您可以查看我的網站:http://apps.stratpoint.com:9114/home/request_verification/ – 2012-01-30 11:56:56