我正在使用 Jquery/Ajax
调用验证我的 html 表单。它是由 Php 处理的。在php页面中,它在处理To Jquery Success方法后返回所有成功结果或错误结果。
所以我想在jquery success方法()中接收另一个参数。有什么办法可以收到吗?
例如:在下面的代码行中如果$numSearch
is == 0
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
然后我想在 jquery success 方法中接收另一个参数,以便我可以加载 getDetails2(SearchValue);
函数。现在,当值为整数
时就会加载。但应该检查值是否为整数并且结果是否显示某些内容。
PHP 代码:
require_once("corefile.php");
$search = (int) $_POST['data'];
$cdid = inputvalid($_POST['cdid']);
if($cdid == "ID"){
// if serach value is empty
if(empty($search)){
echo "<font color='red'>Search keyword required.</font>";
}
// if serach value is not empty
elseif(!empty($search)){
// start myqli_query to search
//$numserach
}
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
}
Jquery 代码:
$(document).ready(function() {
$("#cdid").click(function() {
var SearchValue = $('#txt_name').val();
var cdid = $('#cdid').val();
$("#loading-image").show();
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "doSearch.php",
data : {
'data' : SearchValue,
'cdid' : cdid
},
dataType: "html", //expect html to be returned
success: function(response){
if(Math.floor(SearchValue) == SearchValue && $.isNumeric(SearchValue)){
getDetails2(SearchValue);
}
$("#showSearchResult").html(response);
$('#visiableaftersearch').hide();
document.getElementById('txt_name').value='';
document.getElementById('txt_given_name').value='';
$("#loading-image").hide();
}
});
});
});
最佳答案
如果您使用 JSON,那么您可以在 JSONObject 中添加更多值作为键值对,例如:
{"key1":"value1","key2":"value2"}
然后使用 JSON.parse,解析 success 方法中的数据。然后循环,分别获取key和value。
success: function(response){
var json=JSON.parse(response);
jQuery.each(json, function(i, val) {
console.log("key : "+i+" value: "+val);
});
}
关于javascript - jquery success方法()中如何接收附加参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27120024/