2014-09-10 79 views
0

我的代碼似乎只在我有警報(「文檔加載」)時才起作用。如果我刪除它,它不再工作。它應該把重點放在特定的領域。只有在添加了alert()時,jQuery代碼纔有效

可能是問題,警報(「文檔加載」)給它的代碼時間執行之前,其餘的火災?

<style type='text/css'> 
    /* T */ 
    .comboItemContainer { 
     width: auto; 
     font-family: "Courier New", Courier, monospace; 
    } 

    .colOne { 
     border-right: 1px dashed gray; 
     float: left; 
     width: 100px; 
    } 



    .colTwo_NoDash { 
     float: left; 
     padding-left: 5px; 
     width: 350px; 
    } 


</style> 



<input type="text" name="test" id="test1" /><br /><br /> 
<div id="comboready"></div> 
<div id="comboload"></div> 
<div id="comboOrderTypes"></div> 

    <script> 


    $(document).ready(function() { 

     var data = [ 
        { "ID": 1, "Name": "John Smith", "Age": 45 }, 
        { "ID": 2, "Name": "Mary Johnson", "Age": 32 }, 
        { "ID": 3, "Name": "Bob Ferguson", "Age": 27 } 
     ]; 

     $("#comboready").igCombo({ 
      dataSource: data, //JSON Array defined above 
      valueKey: "ID", 
      textKey: "Name", 
      itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${ID}</div><div  class='colTwo_NoDash'>${Name}</div></div>" 

     }); 


     var data = [ 
     { "ID": 1, "Name": "John S", "Age": 45 }, 
     { "ID": 2, "Name": "Mary J", "Age": 32 }, 
     { "ID": 3, "Name": "Bob F", "Age": 27 } 
     ]; 

     $("#comboload").igCombo({ 
      dataSource: data, //JSON Array defined above 
      valueKey: "ID", 
      textKey: "Name", 
      itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${ID}</div><div class='colTwo_NoDash'>${Name}</div></div>" 

     }); 


     // OrderType Combobox - On Page Load 
     $.get("../order/Json_OrderTypes", function (data) { 
      //Get 
      $("#comboOrderTypes").igCombo({ 
       dataSource: data,    
       valueKey: "Order_Type", 
       textKey: "Order_Type", 
       width: "100px", 
       dropDownWidth: 500, 
       itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${Order_Type}</div><div class='colTwo_NoDash'>${Description}</div></div>" 

      }); 
     }, "json"); 


     alert("document loaded"); 



    }); 




    $(window).load(function() { 

     $("#comboOrderTypes").igCombo("setFocus"); 
     alert("window loaded"); 

    }); 


</script> 
+0

'$ .get'執行異步調用。 'alert()'給它時間來完成,所以你的代碼的其餘部分將工作。 – melancia 2014-09-10 10:24:43

回答

1

插件未初始化直到AJAX請求完成......所以做了ajax成功回調內部setFocus電話。

$(document).ready(function() { 

    var data = [{ 
     "ID": 1, 
      "Name": "John Smith", 
      "Age": 45 
    }, { 
     "ID": 2, 
      "Name": "Mary Johnson", 
      "Age": 32 
    }, { 
     "ID": 3, 
      "Name": "Bob Ferguson", 
      "Age": 27 
    }]; 

    $("#comboready").igCombo({ 
     dataSource: data, //JSON Array defined above 
     valueKey: "ID", 
     textKey: "Name", 
     itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${ID}</div><div  class='colTwo_NoDash'>${Name}</div></div>" 

    }); 


    var data = [{ 
     "ID": 1, 
      "Name": "John S", 
      "Age": 45 
    }, { 
     "ID": 2, 
      "Name": "Mary J", 
      "Age": 32 
    }, { 
     "ID": 3, 
      "Name": "Bob F", 
      "Age": 27 
    }]; 

    $("#comboload").igCombo({ 
     dataSource: data, //JSON Array defined above 
     valueKey: "ID", 
     textKey: "Name", 
     itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${ID}</div><div class='colTwo_NoDash'>${Name}</div></div>" 

    }); 


    // OrderType Combobox - On Page Load 
    $.get("../order/Json_OrderTypes", function (data) { 
     //Get 
     $("#comboOrderTypes").igCombo({ 
      dataSource: data, 
      // selectedItems: [{ index: 5 }, { text: "S" }], 
      valueKey: "Order_Type", 
      textKey: "Order_Type", 
      width: "100px", 
      dropDownWidth: 500, 
      itemTemplate: "<div class='comboItemContainer'><div class='colOne'>${Order_Type}</div><div class='colTwo_NoDash'>${Description}</div></div>" 

     }); 

     //the window load event is not really needed,use it only if you need it for some other purpose 
     $(window).load(function() { 
      $("#comboOrderTypes").igCombo("setFocus"); 
     }); 
    }, "json"); 
}); 
+0

就像一個筆記,我不會調用你的數組「數據」,因爲通常這是一個你會在ajax函數的回調中使用的var。 (就像你做的那樣)。在這種情況下,您會用另一個值覆蓋一個現有值,您可能會遇到將錯誤數據分配給變量的情況。 – Codebryo 2014-09-10 10:29:06

0

用作

setTimeout(function(){$("#comboOrderTypes").igCombo("setFocus");},0); 
相關問題