jQuery常用代码...MVC3 App
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现场演示: http://jsfiddle.net/g3azJ/3/
定义需要一些输入的按钮as required:
#thePageSubmit
是将要禁用的提交按钮,参数是必填字段。这可以用在每个按钮上,或者你拥有的任何按钮上。Live demo: http://jsfiddle.net/g3azJ/3/
Define the button that needs some inputs as required:
#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.