剂量函数在Js中找不到但是可以找到内联原因吗?

时间:2010-12-16 08:00:55

标签: jquery jqgrid asp.net-mvc-3

问题是onkeydown =“doSearch”给出的错误是找不到JS文件中的dosearch函数但是如果我把它内联到它的工作正常。 我做错了吗?

//在JS文件中名为jq.js

jQuery().ready(function () {
    jQuery("#bigset").jqGrid({
        //Grid code
    }).jqGrid('navGrid', '#pagerb', { edit: false, add: false, del: false, search: true }, {}, {}, {}, { multipleSearch: true });

        var timeoutHnd;
        var flAuto = false;

        function doSearch(ev) {
            if (!flAuto)
                return;
            //  var elem = ev.target||ev.srcElement;
            if (timeoutHnd)
                clearTimeout(timeoutHnd)
            timeoutHnd = setTimeout(gridReload, 500)
        }

        function gridReload() {
            var nm_mask = jQuery("#item_nm").val();
            var cd_mask = jQuery("#search_cd").val();
            jQuery("#bigset").jqGrid('setGridParam', { url: "/Jq/bigset?nm_mask=" + nm_mask + "&cd_mask=" + cd_mask, page: 1 }).trigger("reloadGrid");
        }
        function enableAutosubmit(state) {
            flAuto = state;
            jQuery("#submitButton").attr("disabled", state);
        }
    });

// CSHTML页面

@model Learning.Ajax.Models.Order

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="h">Search By:</div>
<div>
    Code<br />
    <input type="text" id="search_cd" onkeydown="doSearch(arguments[0]||event)" />
    <input type="checkbox" id="autosearch" onclick="enableAutosubmit(this.checked)" /> Enable Autosearch <br/>
</div>
<div>
    Name<br />
    <input type="text" id="item_nm" onkeydown="doSearch(arguments[0]||event)" />
    <button onclick="gridReload()" id="submitButton" style="margin-left:30px;">Search</button>
</div>
<br />
<table id="bigset"></table>
<div id="pagerb"></div>


<br /><br />
@*<table id="addgrid"></table>
<div id="pagerad"></div>
<input type="button" id="adddata" value="Add" />
<input type="button" id="editdata" value="Edit Selected" />*@


<script src="@Url.Content("~/Scripts/jq.js")" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:0)

为了更好的练习,你可以像这样改变它,使它们全局化,为什么你把所有内容保存在jQuery就绪函数中....

尝试它应该工作

jQuery().ready(function () {
    jQuery("#bigset").jqGrid({
        //Grid code
    }).jqGrid('navGrid', '#pagerb', { edit: false, add: false, del: false, search: true }, {}, {}, {}, { multipleSearch: true });


    });


var timeoutHnd;
        var flAuto = false;

        function doSearch(ev) {
            if (!flAuto)
                return;
            //  var elem = ev.target||ev.srcElement;
            if (timeoutHnd)
                clearTimeout(timeoutHnd)
            timeoutHnd = setTimeout(gridReload, 500)
        }

        function gridReload() {
            var nm_mask = jQuery("#item_nm").val();
            var cd_mask = jQuery("#search_cd").val();
            jQuery("#bigset").jqGrid('setGridParam', { url: "/Jq/bigset?nm_mask=" + nm_mask + "&cd_mask=" + cd_mask, page: 1 }).trigger("reloadGrid");
        }
        function enableAutosubmit(state) {
            flAuto = state;
            jQuery("#submitButton").attr("disabled", state);
        }