简体   繁体   English

函数为每个交互运行额外的时间

[英]Function runs an additional time for each interaction

I use the following code to create dynamicaly a select menu 我使用以下代码动态创建选择菜单

$('#homecomments').live('pagebeforeshow', function() {
    getTitlesComments();
});

function getTitlesComments() {
    notesdb.transaction(function(t) {
        t.executeSql('SELECT buildingcode, buildingaddress FROM buildings ORDER BY buildingaddress ASC', [], function(t, result) {
            t.executeSql('SELECT DISTINCT buildingcode FROM bill', [], function(t, resultflat) {
                var j,
                    lenflat = resultflat.rows.length,
                    rowflat;
                var i,
                    len = result.rows.length,
                    row;
                //alert(len); 
                if (len > 0 ) {

                    for (i = 0; i < len; i += 1) {
                        row = result.rows.item(i);
                        for (j = 0; j < lenflat; j += 1) {
                            rowflat = resultflat.rows.item(j);
                            if (rowflat.buildingcode == row.buildingcode){
                                 $('#opt1').append('<option value="' + row.buildingcode + '">' + row.buildingaddress + '........' + row.buildingcode + '</option>');
                            }
                        }
                     }
                     $("#opt1").selectmenu('refresh');
                     $("#opt1").change(function() {
                         alert($( "#opt1 option:selected" ).text());
            });
        }


    })
})

Below is my html code of this page 以下是我此页面的html代码

<div data-role="page" id="homecomments">
<div data-role="header">
    <h1>Σχόλια</h1>
    <a href='#home' class='ui-btn-left' data-icon='home' data-theme="a" data-iconpos="notext">Home</a>
</div>
<div data-role="content">
    <div id="entriescomments">
    </div>

    <br>
    <select name="building" id="opt1" data-native-menu="false">
        <option>Πολυκατοικία</option>
    </select>
    <br>
    <select name="flat" id="opt2" data-native-menu="false">
        <option>Διαμέρισμα</option>
    </select>
    <br>
    <input name="flatcommentname" id="flatcommentname" class="flatcomname" type="text" value="" placeholder="Ονοματεπώνυμο"/>

    <div data-role="fieldcontain">
        <label for="textarea"></label>
        <textarea name="flatcomment2" id="flatcomment2" class="flatcom2" placeholder="Παρατήρηση"></textarea>
    </div>  
    <div align="center" style="margin: 25px 0px 0px 0px;">
        <a href="#" id="commentsformbutton"  data-role="button" data-theme="e" >OK</a>
    </div>
</div>

The first time the user visits the page the above code alerts the correct text once. 用户首次访问该页面时,以上代码会一次警告正确的文本。 The second time the user visits the page, after leaving, the code alerts the correct text but twice. 用户离开后,第二次访问该页面时,该代码会警告正确的文本,但是两次。 The third time three times and so on. 第三次第三次,依此类推。

Also the menu the first time has all the options. 此外,菜单第一次具有所有选项。 The second the menu hes all the option but twice and so on. 第二个菜单具有所有选项,但有两次,依此类推。

I try to reset select with 我尝试重置选择

$('select"#opt1 option').removeAttr('selected');

but the problem is not solved. 但是问题没有解决。

Where am I wrong?? 我在哪里错了?

You are attaching the change event toon each pageshow. 您将在每个页面显示上附加更改事件。 Instead, attach it once on pagecreate: 而是在pagecreate上附加一次:

$(document).on("pagecreate", "#homecomments", function(){

    $(document).on("change", "#opt1", function() {
        alert($( "#opt1 option:selected" ).text());
    });

});


$(document).on('pagebeforeshow','#homecomments', function() {
    getTitlesComments();
});

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

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