jQuery代码仅在添加了alert()时才有效

时间:2014-09-10 10:22:09

标签: javascript jquery

我的代码似乎只在我有警报(“文档已加载”)时才起作用。如果我删除它,它就不再起作用了。它应该将setFocus设置在特定的字段上。

可能是什么问题,是警报(“文档加载”)在代码开始之前给代码执行时间?

<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>

2 个答案:

答案 0 :(得分: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");
});

答案 1 :(得分:0)

用作

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