jQuery常用代码...MVC3 App

发布于 2024-12-19 10:52:09 字数 1286 浏览 2 评论 0原文

我有一个 C#.NET MVC3 Web 应用程序,我的页面上有一些常见的 jQuery 功能,并且希望将其模块化。我不知道该怎么做。下面是我正在使用的代码的示例。您会注意到几个控件具有分配给事件的功能。我拥有的每个视图都会执行此操作,但控件(和控件数量)会有所不同。可能有 1 个控件需要添加事件,也可能有 10 个。

$(document).ready(function () {
    $("#Description").keyup(function () {
        disableEnableSave();
    });
    $("#DueDate").change(function () {
        disableEnableSave();
    });
    $("#EndDate").keyup(function () {
        disableEnableSave();
    });
});

下面是disableEnableSave() 代码。类似的问题,在该代码中,可能有 1 个控件需要处理或 10 个控件。

    function disableEnableSave() {
        var text = $("#Description").val();
        var text1 = $("#DueDate").val();
        var text2 = $("#EndDate").val();
        var textlength = text.length;
        var textlength1 = text1.length;
        var textlength2 = text2.length;
        if (textlength > 0 && textlength1 > 0 && textlength2 > 0) {
            $("#thePageSubmit").removeAttr("disabled");
        }
        else {
            $("#thePageSubmit").attr("disabled", "disabled");
        }
        document.title = document.title.replace("*", "");
        document.title = document.title + "*";
        return true;
    }

我不是 jQuery 或 JavaScript 专家,但我认为有一种方法可以将其封装在 .js 文件中并传递一些参数。有什么想法吗?

I have a C#.NET MVC3 web app and I have some common jQuery functionality across my pages and want to modularize it. I don't know how to do this. Below is an example of the code I'm using. You will notice several controls have functions assigned to events. Each View I have will do this, BUT the controls (and number of controls) will be different. There may be 1 control that needs the event added to or there may be 10.

$(document).ready(function () {
    $("#Description").keyup(function () {
        disableEnableSave();
    });
    $("#DueDate").change(function () {
        disableEnableSave();
    });
    $("#EndDate").keyup(function () {
        disableEnableSave();
    });
});

Below is the disableEnableSave() code. Similar issue, inside that code, there may 1 control to work against or 10.

    function disableEnableSave() {
        var text = $("#Description").val();
        var text1 = $("#DueDate").val();
        var text2 = $("#EndDate").val();
        var textlength = text.length;
        var textlength1 = text1.length;
        var textlength2 = text2.length;
        if (textlength > 0 && textlength1 > 0 && textlength2 > 0) {
            $("#thePageSubmit").removeAttr("disabled");
        }
        else {
            $("#thePageSubmit").attr("disabled", "disabled");
        }
        document.title = document.title.replace("*", "");
        document.title = document.title + "*";
        return true;
    }

I am not a jQuery or JavaScript expert but I gotta think there's a way to encapsulate this in a .js file and pass in some parameters. Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-26 10:52:09

现场演示: http://jsfiddle.net/g3azJ/3/

$.fn.disableFalseInput = function(list) {
    var _this = $(this),
        required = $(list);

    required .bind("keyup change", function() {
        var available = true;
        $.each(required , function(k, v) {
            if (v.value.length == 0) {
                available = false;
                return false;
            }
        });
        if (available) {
            _this.removeAttr("disabled")
        } else {
            _this.attr("disabled", "disabled");
        }
    }).first().trigger("keyup");
}

定义需要一些输入的按钮as required:

$("#thePageSubmit").disableFalseInput("#Description, #DueDate, #text2");

#thePageSubmit 是将要禁用的提交按钮,参数是必填字段。这可以用在每个按钮上,或者你拥有的任何按钮上。

Live demo: http://jsfiddle.net/g3azJ/3/

$.fn.disableFalseInput = function(list) {
    var _this = $(this),
        required = $(list);

    required .bind("keyup change", function() {
        var available = true;
        $.each(required , function(k, v) {
            if (v.value.length == 0) {
                available = false;
                return false;
            }
        });
        if (available) {
            _this.removeAttr("disabled")
        } else {
            _this.attr("disabled", "disabled");
        }
    }).first().trigger("keyup");
}

Define the button that needs some inputs as required:

$("#thePageSubmit").disableFalseInput("#Description, #DueDate, #text2");

#thePageSubmit is the submit button that will be disabled, and the parameter are the fields required. This can be used on each button, or whatever you got.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文