繁体   English   中英

用jQuery Mobile向PHP进行ajax调用

[英]ajax call with jQuery Mobile to PHP

我的代码中有问题,我通过ajax调用来调用php,我的回答正确(我通过一些警报测试了json answere),我的问题是当我将数据追加到列表视图时,我没有列表中的数据,即使使用“刷新”也是如此。 您能帮我找到错误吗?

他给了我这个错误:未捕获的错误:初始化之前无法在listview上调用方法; 尝试调用方法“刷新”

这是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);

从HTML中删除“ data-native-menu =“ false”“(默认情况下可能为true),因此HTML中的选择变得简单

<select id="select-subCategory" ></select>

然后列表视图将刷新并显示! :)

您正在使用$('#select-subCategory').append(

ID为selectsubCategory

但应该使用

$('#selectsubCategory').append(

这应该工作正常

$.each(result, function(key, row) {
    $.each(row, function(id, name) {
        $("<option>").val(id).text(name).appendTo($('#selectsubCategory'));
    });
});

$('#select-subCategory').listview();

如果要加载整个列表,则需要初始化它而不是刷新它

我解决了这个问题:

从HTML中删除“ data-native-menu =“ false”“(默认情况下可能为true),因此HTML中的选择变得简单

<select id="select-subCategory" ></select>

然后列表视图将刷新并显示! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM