jQuery最后一次事件发射两次

时间:2012-07-11 14:17:30

标签: javascript jquery ajax

我正在使用以下代码构建jQuery树样式列表。每个API调用(除了最后一个)都会发出N个LI,如下所示。最后一个吐出一张桌子供我放入另一个div。

<li><a data-category="SOMECATEGORY" data-id="SOME ID">SOMETHING</a><ul><!-- AJAX --></ul></li>

在导航树时会发生以下情况:

  • 点击浏览 - 向../api/browse.php?do=getLocations
  • 发送1个HTTP请求
  • 点击其中一个位置 - 向../api/browse.php?do=getFloors&location-id=ID_OF_JUST_CLICKED
  • 发送1个HTTP请求
  • 点击其中一个楼层 - 向../api/browse.php?do=getEquipment&floor-id=ID_OF_JUST_CLICKED
  • 发送1个HTTP请求
  • 点击其中一个设备 - 向../api/browse.php?do=buildTables&location_id=' + browseList['location_id'] + '&floor_id=' + browseList['floor_id'] + '&device_type=' + browseList['device_type']
  • 发送2个HTTP请求

奇怪的是,最终点击事件是两次发射。警告框显示它是被击中两次的同一对象。 这只发生在最后一个元素上。无论有多少台设备,它都会发生。

    var browseList = [];
$("#Browse").click(buildBrowseMenus);

function buildBrowseMenus() {

    if(!this.isOpen) {
     this.isOpen = true;

     $child = $(this).parent().children("ul");

     $child.html('<div class="ui-widget">Loading...</div>');

     //Determine which category we are on.  If we know what category type is clicked
     // We will know which API call to make as the list goes:
     // Browse -> Locations -> Floors -> Equipment Type -> location number

     var category = $(this).data("category");
     switch (category) {

        //First list level, build out the locations
        case undefined:
            $child.load('../api/browse.php?do=getLocations');
        break;

        //Second list level, build out the floors for a given location
        case 'locations':
            browseList['location_id'] = $(this).data("id");
            $child.load('../api/browse.php?do=getFloors&location-id=' + $(this).data("id"));
        break;

        //Third list level, build out the Equipment for a given floor
        case 'floors':
            browseList['floor_id'] = $(this).data("id");
            $child.load('../api/browse.php?do=getEquipment&floor-id=' + $(this).data("id"));
        break;

        //If one of the pieces of equipment is clicked build the main tables
        case 'equipment':
            alert(JSON.stringify(this));
            browseList['device_type'] = $(this).data("id");
            $child.html('');
            $("#listStuffBody").load('../api/browse.php?do=buildTables&location_id=' + browseList['location_id'] + '&floor_id=' + browseList['floor_id'] + '&device_type=' + browseList['device_type']);
            return;
        break;
     }

     $child.delegate("a","click",buildBrowseMenus);

    }else{
      var $child = $(this).parent().children("ul");
      $child.html('');
      this.isOpen = false;

    }
}

问题是什么?最后一次点击只能触发一次。

编辑:::当点击(浏览 - >位置1-&gt;第一层 - >汽车)时,DOM如下

<li>
    <a id="Browse">Browse</a>
    <ul>
        <li>
        <a data-id="2" data-category="locations">LOCATION 1</a>
        <ul>
            <li>
            <a data-id="2" data-category="floors">First Floor</a>
            <ul>
                <li>
                <a data-id="truck" data-category="equipment">truck</a>
                <ul><!-- -- AJAX ----></ul>
                </li>

                <li>
                <a data-id="car" data-category="equipment">car</a>
                <ul><div class="ui-widget">Loading...</div></ul>
                </li>

                <li><a data-id="boat" data-category="equipment">voat</a>
                <ul><!-- -- AJAX ----></ul>
                </li>
            </ul>
            </li>
        </ul>
        </li>

        <li>
        <a data-id="1" data-category="locations">Location 2</a>
        <ul><!-- -- AJAX ----></ul>
        </li>
    </ul>
</li>

编辑::不同的路径(位置2)

<li>
<a id="Browse">Browse</a>
<ul>
    <li>
    <a data-id="2" data-category="locations">Location 1</a>
    <ul><!-- -- AJAX ----></ul>
    </li>

    <li>
    <a data-id="1" data-category="locations">Location 2</a>
    <ul>
        <li>
        <a data-id="3" data-category="floors">First Floor</a>
        <ul>
            <li>
            <a data-id="truck" data-category="equipment">truck</a>
            <ul></ul>
            </li>

            <li>
            <a data-id="car" data-category="equipment">car</a>
            <ul></ul>
            </li>

            <li>
            <a data-id="boat" data-category="equipment">boat</a>
            <ul><!-- -- AJAX ----></ul>
            </li>
        </ul>
        </li>

        <li>
        <a data-id="4" data-category="floors">Second Floor</a>
        <ul><!-- -- AJAX ----></ul>
        </li>

        <li>
        <a data-id="5" data-category="floors">Third Floor</a>
        <ul><!-- -- AJAX ----></ul>
        </li>

        <li>
        <a data-id="6" data-category="floors">Fourth Floor</a>
        <ul><!-- -- AJAX ----></ul>
        </li>
    </ul>
    </li>

</ul>
</li>

0 个答案:

没有答案