jQuery 通用代码
我在应用程序中使用 jQuery 模式对话框来处理正常的 CRUD 操作。在某些情况下,我什至打开了两个堆叠模式对话框。
然后,我在外部 javascript 文件中创建了两个通用函数,分别处理 CRUD 表单的显示和提交。
为了显示模式对话框,我调用以下函数。
function _loadDialog(level, action, id, title, onCloseHandler) {
var panel = panels[level];
$(panel).dialog("option", "title", title);
var url = action;
if (id != "") url = url + "/" + id;
$.ajax({
type: "get",
dataType: "html",
url: url,
data: {},
success: function(response) {
$(panel).html('').html(response).dialog('open');
}
});
$(panel).unbind("dialogclose").bind("dialogclose", function(event, ui) {
if (onCloseHandler != null) {
onCloseHandler();
}
});
}
该函数接收一个 level
参数,该参数指示该函数如何堆叠对话框以及在哪里放置从 ajax 调用返回的渲染的部分标记。这个功能运行良好。
在 ajax 调用返回的部分视图内有输入,最后是以下代码
<div style="text-align:right;">
<input type="submit" id="btnSave" value="Salva" />
</div>
,对于 jQuery 部分,例如,
$("#contactForm").submit(function(event) {
_submitForm(1, event, "#contactForm", "post", "html", '<%= Url.Content("~/Contact/Save") %>');
});
如您所见,提交函数具有以下签名
function _submitForm(level, event, formName, atype, adataType, aurl) {
}
,它处理
- 表单提交到正确的控制器操作
- 用户反馈(例如“操作执行成功”)
- 对话框关闭操作
需要级别参数来将所有功能(包括关闭对话框)寻址到所使用的正确 DIV 面板。
我希望能够有时将同一对话框显示为对话框,有时显示为子对话框。
为了能够做到这一点,我的意思是“从逻辑上讲”,因为我对 javascript 和 jQuery 不太熟悉,我需要进行以下更改:
- 修改
_loadDialog
函数以保存level 参数
- 修改
_submitForm
函数并使其使用之前从_loadDialog
函数保存的正确level
参数。
我怎样才能实现这个目标?
I am using jQuery modal dialogs in my app to handle normal CRUD operations. In some cases I have even two stacked modal dialogs open.
I have then created two generic function in an external javascript file to handle respectively the showing and the submit of CRUD forms.
To show modal dialogs I call the following function
function _loadDialog(level, action, id, title, onCloseHandler) {
var panel = panels[level];
$(panel).dialog("option", "title", title);
var url = action;
if (id != "") url = url + "/" + id;
$.ajax({
type: "get",
dataType: "html",
url: url,
data: {},
success: function(response) {
$(panel).html('').html(response).dialog('open');
}
});
$(panel).unbind("dialogclose").bind("dialogclose", function(event, ui) {
if (onCloseHandler != null) {
onCloseHandler();
}
});
}
This function receive, among the others, a level
parameter that instruct the function how to stack the dialog and where to place the rendered Partial markup returning back from the ajax call. This function works fine.
Inside the Partial View returned by the ajax call there are input and at the end the following code
<div style="text-align:right;">
<input type="submit" id="btnSave" value="Salva" />
</div>
and, for the jQuery part, for example,
$("#contactForm").submit(function(event) {
_submitForm(1, event, "#contactForm", "post", "html", '<%= Url.Content("~/Contact/Save") %>');
});
As you can see the submit function has the following signature
function _submitForm(level, event, formName, atype, adataType, aurl) {
}
and it handle
- the form submission to the correct controller action
- the user feedback (e.g. "action executed successfully")
- the dialog close operation
The level parameter is needed to address all the function, including closing the dialog, to the correct DIV panel used.
I would like to be able to show the same dialog sometimes as a dialog and sometimes as a sub dialog.
To be able to do this, I mean "logically speaking" as I am not so strong with javascript and jQuery, I need the following changes:
- Modify the
_loadDialog
function to save thelevel
parameter inside the dialog markup itself - Modify the
_submitForm
function and make it using the correctlevel
parameter that has been previously saved from the_loadDialog
function.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对什么在哪里、什么时间有点困惑,但似乎你想将一些信息与特定元素相关联。
为此,您可以使用 jQuery 的
.data()
方法。它将把您想要的任何数据与特定元素关联起来。不确定在您的情况下应该如何使用它,但这里有一个通用示例:
现在,ID 为
someSelector
的元素将把数字3
映射到其其关联数据中的“level”属性。
因此,要检索它,您可以这样做:
当销毁具有与其关联的数据的元素时(这只是此类数据的一个示例),您应该确保使用
.remove()
或 < code>.empty() 或自动或手动删除数据的其他方法之一。I'm a little confused about what is where and when, but it seems that you want to associate a bit of information with a particular element.
To do this, you can use jQuery's
.data()
method. It will associate whatever data you want with a particular element.Not sure how exactly it should be used in your situation, but here's a generic example:
Now the element with the ID
someSelector
will have the number3
mapped to its'level'
attribute in its associated data.So to retrieve it, you would do this:
When destroying an element that has data associated with it (this is just one example of such data), you should be sure to use
.remove()
or.empty()
or one of the other methods that either automatically or manually removes the data.听起来您所需要的只是在调用它时将您已经获得的
level
参数传递给_submitForm
函数。虽然我显然跳过了我不知道您的实现的所有细节,但关键是无论传递到
_loadDialog
的level
稍后都会传递到 < code>_submitForm 被调用时。在 JavaScript 中,调用
_loadDialog
的同时不调用_submitForm
并不重要。通过所谓的“闭包”(JavaScript 程序员喜欢谈论的东西),level
变量被安全地保存在form.submit()
的匿名回调函数中,并且将当最终调用该函数时,仍然可以使用相同的值。It sounds like all you need is to pass on the
level
parameter you've already got to the_submitForm
function when calling it.While I've obviously skipped over all the details I don't know of your implementation, the key is simply that whatever
level
gets passed into_loadDialog
will later get passed into_submitForm
when it's called.In JavaScript, it doesn't matter that
_submitForm
isn't called at the same time you call_loadDialog
. Through what's called a "closure" (something JavaScript programmers love to talk about), thelevel
variable gets saved safely inside your anonymous callback function forform.submit()
and will still be available with the same value when that function is eventually called.