簡體   English   中英

jQuery onChange事件在我的jQuery移動頁面上觸發了多次

[英]jquery onChange event is firing more than once on my jquery mobile page

我的使用jquery onChange函數的JS代碼在似乎應該僅觸發一次時被觸發多次。

我有四個jquery移動單選按鈕,當您單擊其中一個時,將對單擊的單選按鈕的值進行評估,然后重寫頁面URL以刪除/添加正確的url字符串,然后通過AJAX通過我的loadResults(url)函數,然后顯示更新的內容。

第一次單擊其中一個單選按鈕時,這似乎按預期方式工作,但是此后,似乎反復調用了loadResults,但我不知道為什么。

這是我的單選按鈕的html代碼:

        <fieldset data-role="controlgroup" id="proptype">
          <!--{if ($searchCriteria.openHouseSearch != 1 || $searchCriteria.latest != 1 )}-->              
                <input type="radio" name="propStatus" id="propStatus-active" value="activelistings" checked>
          <!--{else}-->
                <input type="radio" name="propStatus" id="propStatus-active" value="activelistings">
          <!--{/if}-->
          <label for="propStatus-active">Active Listings</label>      

          <!--{if ($searchCriteria.latest == 1 || $searchCriteria.propStatus == latest) }-->
                <input type="radio" name="propStatus" id="propStatus-new" value="latest" checked>
          <!--{else}-->
                <input type="radio" name="propStatus" id="propStatus-new" value="latest">
          <!--{/if}-->
          <label for="propStatus-new">New Listings</label>

          <!--{if $searchCriteria.openHouseSearch == 1}-->
                <input type="radio" name="propStatus" id="propStatus-openHouse" value="openHouseSearch" checked>
          <!--{else}-->
                <input type="radio" name="propStatus" id="propStatus-openHouse" value="openHouseSearch">
          <!--{/if}-->
            <label for="propStatus-openHouse">Open Houses</label>

          <!--{if $searchCriteria.areaSearch == 1}-->
                <input type="radio" name="propStatus" id="propStatus-areaSearch" value="areaSearch" onchecked>
          <!--{else}-->
                <input type="radio" name="propStatus" id="propStatus-areaSearch" value="areaSearch">
          <!--{/if}-->
            <label for="propStatus-areaSearch">Area Search</label>

        </fieldset>

這是我的jQuery javascript代碼:

$ j(document).on('pagebeforeshow','#resultspage',function(){

//alert('this is fired'); 

    $j('#proptype input[type=radio]').change(function(e){
        //alert(url);
        //alert ('RADIOBUTTON VALUE IS: ' +$j(this).val() )
                var url = window.location.href; 
                if ($j(this).val() == "latest") { // New Listings
                    //alert('This is a new listings search');
                    document.getElementById('areainformation').style.display = 'none';
                    document.getElementById('resultsContents').style.display = 'block';
                    url = url.replace(/&activelistings=1/g,'');
                    url = url.replace(/&openHouseSearch=1/g,'');
                    url = url.replace(/&page=[0-9]+/g,'');
                    url = url.replace(/&areaSearch=1/g,'');
                    url += "&latest=1";
                    //alert('latest is value: ' +url);
                    loadResults(url);
                } else if ($j(this).val() == "openHouseSearch") { // Open Houses
                    // WHY IS THIS FIRED MULTIPLE TIMES AFTER YOU DO A NEW LISTINGS SEARCH AND THEN CHOOSE AN OPENHOUSE SEARCH??
                    //alert('This is an openHouse search');
                    document.getElementById('areainformation').style.display = 'none';
                    document.getElementById('resultsContents').style.display = 'block';
                    url = url.replace(/&activelistings=1/g,'');
                    url = url.replace(/&latest=1/g,'');
                    url = url.replace(/&propStatus=latest/g,'');
                    url = url.replace(/&page=[0-9]+/g,'');
                    url = url.replace(/&areaSearch=1/g,'');
                    url += "&openHouseSearch=1";
                    //alert('openHouseSearch is value: ' +url);
                    loadResults(url);
                } else { // All Active Listings or Area Search
                    if ($j(this).val() == "areaSearch") { // Area Search
                        //$j('#areainformation').style.display = 'block';
                        //$j('#resultsContents').style.display = 'none';
                        //alert('This is an area search');
                        document.getElementById('areainformation').style.display = 'block';
                        document.getElementById('resultsContents').style.display = 'none';
                    } else {
                        //alert('This is an all active listings search');
                        document.getElementById('areainformation').style.display = 'none';
                        document.getElementById('resultsContents').style.display = 'block';
                        url = url.replace(/&openHouseSearch=1/g,'');
                        url = url.replace(/&latest=1/g,'');
                        url = url.replace(/&propStatus=latest/g,'');
                        url = url.replace(/&page=[0-9]+/g,'');
                        url = url.replace(/&areaSearch=1/g,'');
                        url += "&activelistings=1";
                        //alert('doing activelisting: ' +url);
                        loadResults(url);
                    }
                }
      });

});

我猜您會從其他單選按鈕中收到更改事件。 使用單選按鈕組,您可以設置一個值並清除當前選定的值(這不會第一次發生,因為尚未選擇任何內容)。

您可能可以通過取消事件來解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM