开发者

passing function or template into jQuery file

开发者 https://www.devze.com 2023-03-04 17:47 出处:网络
I have a jQuery file handling a control that can have multiple instances on the page. Each instance needs to pass in a handler function and a template to the control.

I have a jQuery file handling a control that can have multiple instances on the page. Each instance needs to pass in a handler function and a template to the control. The server side i开发者_JS百科s in .net / C# A code snippet would be appreciated.

[Edit] on the page I have:

<script type="text/javascript">
    $("#<%=AutoSearch1.ClientID %>").bind('AutoSearch_Changed', function (e, data) {
        if (data == "") {
            $('#<%=cbUntilOnDate.ClientID %>').attr("checked", true);
            $('#<%=cbUntilEndofProgram.ClientID %>').attr("disabled", true);
        }

        else
            $('#<%=cbUntilEndofProgram.ClientID %>').attr("disabled", false);
    });
</script>

<uc1:AutoSearch  runat="server" ID="AutoSearch1"  
    OnTextChangeFuction = "AutoSearch_Changed"/>

In the control:

public string OnTextChangeFuction { 
    get {}; 
    set {};
}

function ClearAutoSearch() {
    $('#<%=txtSearch.ClientID %>').val('');
    $('#divNoResults').hide();
    $('#divSeeAllResults').hide();
    $('#AutoSearchResults').hide();
    $('#<%=HiddenField1.ClientID %>').val('');

    if ("<%=OnTextChangeFuction %>" != "")
        $("#<%=txtSearch.ClientID %>").trigger("<%=OnTextChangeFuction %>",             
            $('#<%=HiddenField1.ClientID %>').val()); 
}

I want to move control scripts out into a separate .js file. How do I pass the function into a file?


You don't "pass a function to a file". You can use your server to dynamically build javascript files and write whatever data values in to them you want. However, this may not be the best solution. If you have a function which needs to be called and depends on data supplied by the server, it's may be best to have your javascript functions in a flat js file and use the server to just supply javascript data when it generates your html file. So you would do something like this:

// in your js file
function ClearAutoSearch(searchFieldIDS) {
   $(searchFieldIDS.textSearchID).val('');
   $(searchFieldIDS.noResultsID).hide();
    // etc
}

// in your server-generated html file
<script>
    // use the server to write javascript which will build a data object
    var searchFieldIDS = {
        textSearchID: '#<%=txtSearch.ClientID %>',
        noResultsID: '#divNoResults' 
    }

    // pass that data object to your function
    ClearAutoSearch(searchFieldIDS)
</script>

Note that in my example, searchFieldIDS is a global variable. I've kept it this way for simplicity, but in production code, I avoid global javascript variables. You avoid global variables by creating your own top-level namespace and putting your variables in there (MyNameSpace.searchFieldIDs), or you can declare them in a non-global scope, like within the body of jquery's document.ready

0

精彩评论

暂无评论...
验证码 换一张
取 消