0

jquery モバイルのリストビューに問題があります。

リストビューは動的に構築されます。onclick li item イベントは発生せず、エラーは発生しませんでした。

Jquery 関数

function GetDependents() {
    var userid= checkCookie1(); 

    "use strict";
    var wcfServiceUrl = "http://xcxcxcx/PHRService/Service1.svc/XMLService/";
    $.ajax({
        url: wcfServiceUrl + "Dependents",
        data: "PatientID=" + userid + "",
        type: "GET",
        processData: false,
        contentType: "application/json",
        timeout: 10000,
        dataType: "json",
        cache:false,
        beforeSend: function (xhr) {
            $.mobile.showPageLoadingMsg();
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        },
        complete: function () {

            $.mobile.hidePageLoadingMsg();
        },

        success: function (data) {
            var result = data.GetDependentsResult;
            var items = [];

            $.each(result, function (i, item) {

                items.push('<li><a onclick=redirect(\'AddDependents.htm?id=' + item.ItemID + '\');\>' + item.ItemName + '</a></li>');
            });
            $('#main_list').append(items.join(''));
            $('#main_list').listview('refresh');
        },
        error: function (data) {

        }
    });

}

そしてHTMLは

<div data-role="content" data-theme="c">
            <ul data-role="listview" id="main_list">
            </ul>
            <br />
            <a onclick="redirect('AddDependents.htm');" data-role="button" data-icon="plus">Add new...</a>
        </div>

この問題を解決するのを手伝ってください。

4

1 に答える 1

3

イベントの呼び出しは、インライン JavaScript を使用するよりも優れています。

これを試して

HTML

items.push('<li><a id="'+ item.ItemID + '">' + item.ItemName + '</a></li>');

Jクエリ

$('#main_list').on('click','a',function(e){
    e.preventDefault();
    var Id=$(this).attr('id');
    redirect('AddDependents.htm?id=' +Id );
});

要素を追加しているため、動的にon委任されたイベントを使用する必要があります..

注:<a>タグ の余分な終了が表示されました

<a onclick=redirect(\'AddDependents.htm?id=' + item.ItemID + '\');\>'
                                                             //---^^---here

余分な\is 終了タグがあります<a/>

于 2013-03-28T16:38:09.200 に答える