javascript函数不在document.ready中调用

时间:2014-02-12 13:15:21

标签: javascript jquery jqwidget

我有两个函数可以生成两个带有jqwidgets库的组合框。两个功能大多是彼此相似的。我在$(document).ready(function ()中调用它们,但第一个功能运行良好,第二个功能甚至没有调用。我将代码放在一个函数中,但第二部分从不运行。

function f1() {
     var url2 = "/autosuggest/JsonOrigins.aspx";

     var source2 = {
         datatype: "json",
         datafields: [{
             name: 'id'
         }, {
             name: 'name'
         }],
         url: url2,
         async: false
     };
     var dataAdapter2 = new $.jqx.dataAdapter(source2);
     // Create a jqxComboBox
     $("#originsjqxWidget").jqxComboBox({
         source: dataAdapter2,
         multiSelect: true,
         displayMember: "name",
         valueMember: "id",
         width: 145
     });


     $("#arrow").jqxButton({});
     $("#arrow").click(function () {
         $("#originsjqxWidget").jqxComboBox({
             showArrow: false
         });
     });

     $("#originsjqxWidget").on('change', function (event) {
         var items = $("#originsjqxWidget").jqxComboBox('getSelectedItems');
         var selectedItems = "Selected Items: ";
         $.each(items, function (index) {
             selectedItems += this.label;
             if (items.length - 1 != index) {
                 selectedItems += ", ";
             }
         });
         $("#log").text(selectedItems);
     });
 };

 function f2() {
     var url = "/autosuggest/JsonTag.aspx";

     var source = {
         datatype: "json",
         datafields: [{
             name: 'id'
         }, {
             name: 'name'
         }],
         url: url,
         async: false
     };
     var dataAdapter = new $.jqx.dataAdapter(source);

     $("#categoriesjqxWidget").jqxComboBox({
         source: dataAdapter,
         multiSelect: true,
         displayMember: "name",
         valueMember: "id",
         width: 145
     });


     $("#arrow").jqxButton({});
     $("#arrow").click(function () {
         $("#jqxComboBox").jqxComboBox({
             showArrow: false
         });
     });

     $("#categoriesjqxWidget").on('change', function (event) {
         var items = $("#categoriesjqxWidget").jqxComboBox('getSelectedItems');
         var selectedItems = "Selected Items: ";
         $.each(items, function (index) {
             selectedItems += this.label;
             if (items.length - 1 != index) {
                 selectedItems += ", ";
             }
         });
         $("#log").text(selectedItems);
     });
 }


 $(document).ready(function () {

     f1();
     f2();

 });

3 个答案:

答案 0 :(得分:0)

删除;在你的第一个功能之后。

答案 1 :(得分:0)

试试这个

var source2 = {
     datatype: "json";
     datafields: [{
         name: 'id'
     }, {
         name: 'name'
     }],
     url: url2,
     async: false
 };

答案 2 :(得分:-3)

我认为你有额外的;在函数f1的末尾

        function f1(){ 
...
                 if (items.length - 1 != index) {
                     selectedItems += ", ";
                 }
             });
             $("#log").text(selectedItems);
         });
     };