简体   繁体   English

如何将事件绑定到jQuery函数

[英]how to jquery event bind to function

<input id='btnExcelRead' name='btnExcelRead' type='submit' runat='server'/>   <- actually asp:button
<input id='excelUpload' name='excelUpload' type='file' />   
<input id='txtStartDate' type='text' />
<input id='txtEndDate' type='text' />

.. ..

$(function(){

          $("#btnExcelRead").click(CheckValidation);

        });

        var CheckValidation = function() {
            if ($("#excelUpload").val() === "") {
                alert("Select file");
                return false;
            }
            if ($("$txtStartDate").val() === "") {
                alert("Check the start date!");
                return false;
            }
            if ($("$txtEndDate").val() === "") {
                alert("Check the end date!");
                return false;
            }
        }

here i made simple jquery code. 在这里,我做了简单的jQuery代码。

I want to bind function when btnExcelRead button click. 当btnExcelRead按钮单击时,我想绑定功能。

is this originally wrong way? 这本来是错误的方式吗?

What you have is valid, aside from the selectors, I would reformat is a bit, like this: 您拥有的是有效的,除了选择器之外,我将重新格式化,如下所示:

$(function(){
      $("#btnExcelRead").click(CheckValidation);
});

function CheckValidation () {
    if ($("#excelUpload").val() === "") {
        alert("Select file");
        return false;
    }
    if ($("#txtStartDate").val() === "") {
        alert("Check the start date!");
        return false;
    }
    if ($("#txtEndDate").val() === "") {
        alert("Check the end date!");
        return false;
    }
}

You can see a demo of it working here 您可以在这里看到它的演示

You have $txtStartDate and $txtEndDate for your selectors, I think you meant #txtStartDate and #txtEndDate here (I assume you're finding them by ID). 您的选择器有$txtStartDate$txtEndDate ,我想您的意思是这里的#txtStartDate#txtEndDate (我想您是通过ID查找它们的)。 Also if you want a named function, just make one :) If you store a variable pointing to a anonymous function, make sure to put a ; 另外,如果您想要一个命名函数,只需创建一个:)如果您存储一个指向匿名函数的变量,请确保放置一个; after it, since that's a statement. 在那之后,因为这是一个声明。

$("#btnExcelRead").click(function(){CheckValidation()});

You have to declare the callback before calling it like follows: 您必须在调用之前声明回调,如下所示:

    var CheckValidation = function() {
        if ($("#excelUpload").val() === "") {
            alert("Select file");
            return false;
        }
        if ($("$txtStartDate").val() === "") {
            alert("Check the start date!");
            return false;
        }
        if ($("$txtEndDate").val() === "") {
            alert("Check the end date!");
            return false;
        }
    }

    $(function(){

      $("#btnExcelRead").click(CheckValidation);

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM