简体   繁体   English

用jQuery Mobile向PHP进行ajax调用

[英]ajax call with jQuery Mobile to PHP

I have a problem in my code, I call the php by an ajax call, I have the right answere (I tested the json answere by some alerts), my problem is when I append the data to my list-view, I have no data in my list even using the "refresh". 我的代码中有问题,我通过ajax调用来调用php,我的回答正确(我通过一些警报测试了json answere),我的问题是当我将数据追加到列表视图时,我没有列表中的数据,即使使用“刷新”也是如此。 Can you help me to find the bug please. 您能帮我找到错误吗?

He gives me this error: Uncaught Error: cannot call methods on listview prior to initialization; 他给了我这个错误:未捕获的错误:初始化之前无法在listview上调用方法; attempted to call method 'refresh' 尝试调用方法“刷新”

Here the code in HTML and jQuery 这是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>

And this is my php file 这是我的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 从HTML中删除“ data-native-menu =“ false”“(默认情况下可能为true),因此HTML中的选择变得简单

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

then the listview will refresh and appear!! 然后列表视图将刷新并显示! :) :)

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

and id is selectsubCategory ID为selectsubCategory

but should be used 但应该使用

$('#selectsubCategory').append(

this should work fine 这应该工作正常

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

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

if you are loading the entire list you will need to initialize it not to refresh it 如果要加载整个列表,则需要初始化它而不是刷新它

I fixed the problem : 我解决了这个问题:

Delete 'data-native-menu="false"' from HTML, (maybe is true by default), so the select in HTML become simply 从HTML中删除“ data-native-menu =“ false”“(默认情况下可能为true),因此HTML中的选择变得简单

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

then the listview will refresh and appear!! 然后列表视图将刷新并显示! :) :)

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

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