我在我的代碼中遇到問題,我通過ajax調用了php,我有正確的答案(我通過一些提示測試了json answere),我的問題是當我追加數據到我的列表視圖中,即使使用「刷新」,我的列表中也沒有數據。你能幫我找到bug嗎?ajax調用jQuery Mobile到PHP
他給了我這個錯誤: 未捕獲錯誤:在初始化之前無法在列表視圖上調用方法;試圖調用方法 '刷新'
在這裏,在HTML代碼和jQuery
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$.ajax({url: "SubCategory.php",
dataType: "json",
jsonpCallback: 'successCallback',
async: true,
success: function (result) {
ajax.parseJSONP(result);
},
error: function (request,error) {
alert('Network error has occurred please try again!');
}
});
var ajax = {
parseJSONP:function(result){
$.each(result, function(i, row) {
$('#select-subCategory').append('<option value="'+row.id+'">Annuncio: '+row.name+'</option>');
});
$('#select-subCategory').listview('refresh');
}
}
});
</script>
<body>
<form method="post" action="SubCategory.php">
<select id="select-subCategory" data-native-menu="false">
</select>
</form>
</body>
這是我的PHP文件
class SCategory
{
public $id;
public $name;
}
$SubCategories = array();
$SubCategories[0] = new SCategory;
$SubCategories[0]->id = 0;
$SubCategories[0]->name = 'first';
$SubCategories[1] = new SCategory;
$SubCategories[1]->id = 1;
$SubCategories[1]->name = 'second';
$SubCategories[2] = new SCategory;
$SubCategories[2]->id = 2;
$SubCategories[2]->name = 'third';
$SubCategories[3] = new SCategory;
$SubCategories[3]->id = 3;
$SubCategories[3]->name = 'fourth';
echo json_encode($SubCategories);
SOLUTION
Delete 'data-native-menu="false"' from HTML, (maybe is true by default), so the select in HTML become simply
<select id="select-subCategory" ></select>
then the listview will refresh and appear!! :)
'select'不是'listview',你應該使用'.selectmenu(「refresh」)'。 – Omar
我在本週末修復了這個問題,也許你是對的@Omar,我的解決方案是從HTML中刪除'data-native-menu =「false」',所以select變成 –