[英]jQuery Json + jQuery onChange
I am using php to talk to a mysql database then encoding the associative array with json_encode. 我使用php与mysql数据库交谈,然后使用json_encode对关联数组进行编码。 That much I know is working, I can call the php directly and see the json result.
我知道的很多,我可以直接调用php并查看json结果。
With jQuery I am using onChange on a dropdown to call the php function and to get the results. 使用jQuery我在下拉列表上使用onChange来调用php函数并获得结果。
I think I have problems with the jQuery and json, but I have looked through multiple posts and cannot find a good answer. 我认为我有jQuery和json的问题,但我查看了多个帖子,但找不到一个好的答案。
Here is the code I am using. 这是我正在使用的代码。 How do I get to the json array?
我如何进入json数组? I have tried data['ele'], data.ele, data[0] and all return undefined.
我已经尝试了数据['ele'],data.ele,data [0]和所有返回undefined。
$('#itemDD').change(function(data){
alert("Changing");
askServer(function(data){
alert("!" + data['retailListPrice']);
//spin through the data and put the results in workingQuote
});
});
function askServer(callback)
{
var selItem = $('#itemDD').val();
var strUrl = "getItemList.php?item=" + selItem + "&action=itemInfo";
alert(strUrl);
jQuery.ajax({
url:strUrl,
success:callback,
dataType: "json"
});
}
});
PHP PHP
$sql="SELECT *
FROM items
WHERE modelNum='" . $selectedModel . "'";
$result = mysql_query($sql,$conn) or die("Error" . mysql_error());
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray));
echo json_encode($resultArray);
try this (note: object/array format is diffrent in JS): 试试这个(注意:对象/数组格式在JS中是不同的):
function askServer()
{
var selItem = $('#itemDD').val();
var strUrl = "getItemList.php?item=" + selItem + "&action=itemInfo";
alert(strUrl);
$.get(strUrl, function(data){
alert(data.retailListPrice);
});
}
Inside change event just call the askServer()
function and declare it outside of change event, like 在更改事件内部只需调用
askServer()
函数并在change事件之外声明它,例如
$('#itemDD').change(function(data){
//...
askServer();
});
and your askServer()
function 和你的
askServer()
函数
function askServer()
{
var selItem = $('#itemDD').val();
var strUrl = "getItemList.php?item=" + selItem + "&action=itemInfo";
$.get(strUrl, function(data){
$.parseJSON(data);
alert(data.retailListPrice);
});
}
You using array and mysql_fetch_assoc. 您使用array和mysql_fetch_assoc。 So to get the data, use data[0]['rowName'] or data[0].rowName
因此,要获取数据,请使用data [0] ['rowName']或data [0] .rowName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.