如何清空ul自动完成JqueryMobile

时间:2013-04-19 08:24:03

标签: javascript jquery autocomplete html-lists

我有一个使用ul元素进行自动完成的网站(使用JQueryMobile)。 我第一次加载页面时,我可以输入一些文本到ul输入(ul with data-role listview),它将保存在db中。 然后,我重定向到其他网站,但当我再次转向同一个网站时,我在ul输入中输入的数据仍然存在!当我将它发送到db时它会显示一个错误(错误,因为我需要在发送之前清空字段)。

我在为网络充电时如何清空ul字段?我试过

$("ul").html(" "); //not work
$("#id_div").remove(); //destroy the web and I can't enter again
$("#id_div").empty(); //same as above

谢谢,抱歉我的英语不好!

编辑:

我有不同的功能:

$( document ).on( "pageinit", "#confirm", function() {

    if(typeof localStorage.emails === 'undefined' || localStorage.emails === null){
        var mail = [];
        localStorage.emails = JSON.stringify(mail);
    }


    $( "#confirm_email1" ).on( "listviewbeforefilter", function ( e, data ) {
        var $ul = $( this ),
        $input = $( data.input ),
        value = $input.val(),
        html = "";
        $ul.html( "" );
        if ( value && value.length > 1 ) {

            var emails = JSON.parse(localStorage.emails)
            for(var i=0;i < emails.length;i++){
                html += "<li>" + emails[i] + "</li>";
            }
            $ul.html( html );
            $ul.listview( "refresh" );
            $ul.trigger( "updatelayout");
            $('li').click(function(){
                var mail = $(this).text();
                $input.val(mail);
                $ul.html( "" );
                $ul.listview( "refresh" );
                save_email1="";
                enviar_email1=mail;
            });

            save_email1 = $input.val();
            enviar_email1 = save_email1;
        }
    });

使用此功能,我创建localStorage以将li元素添加到自动完成中。

然后,我使用另一个:

function enviarReserva(){

    if(save_email1!==""){
        var emails = JSON.parse(localStorage.emails);
        emails.push(save_email1);
        localStorage.emails=JSON.stringify(emails);
    }

    var horafi = parseInt(horaReserva)+1;

    var check_soci1;

    if((enviar_email1!='' ) && (document.getElementById("soci_nosoci1").checked!=true)){
        check_soci1="no";
    }else{
        check_soci1="yeh";
    }

    $.ajax ( {
        beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
        complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
        url: "someURL?email1="+enviar_email1+"&check1="+check_soci1,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
            alert(data);
            actualitzarReserves();
            $.mobile.changePage("#mon",{transition:'none'});
            pag = 0;
            $('.pagina').html(pag);
            chargeWallet();
        },
        error: function (data, textStatus, jqHXR) {
            window.location.href="index.html";
        }
    });
}

此函数检查ul是否为空以及是否选中了检查。然后,如果check_soci为no,则PHP返回一些内容,如果check_soci为yeh,则返回其他内容。

我的问题是我第二次进入同一个网页时,默认情况下ul不是空的,虽然我清空输入但它发送给PHP的check_soci为no。 ERROR !!

0 个答案:

没有答案
相关问题