yui 提交按钮和调度操作
我在我的项目中使用 YUI 2.7.0 和 Struts,并有一个带有两个提交按钮的表单。一个用于保存,另一个用于删除。表单提交给 DispatchAction。我已成功添加 onclick 侦听器,但问题是表单在侦听器执行之前提交。我需要在表单提交之前设置调度参数。我该怎么做?以下是我到目前为止所拥有的:
<form name="adminUserForm" method="post" action="manage_user.do">
<input type="hidden" id="dispatch" name="dispatch" value="unspecified"/>
<!-- other form fields here -->
<input type="submit" value="Save User" id="saveUser">
<input type="submit" value="Delete User" id="deleteUser">
</form>
<script type="text/javascript">
function confirmDelete(msg)
{
if (confirm(msg))
{
return setDispatchMethod("delete");
}
else
return false;
}
function setDispatchMethod(methodName)
{
dispatchField = document.getElementById("dispatch");
dispatchField.value = methodName
return true;
}
var onClickSaveUser = function (e)
{
return setDispatchMethod('save');
};
var onClickDeleteUser = function (e)
{
return confirmDelete('Are you sure you want to delete the user?');
};
new YAHOO.widget.Button("deleteUser", {onclick: {fn: onClickDeleteUser }});
new YAHOO.widget.Button("saveUser", {onclick: {fn: onClickSaveUser }});
</script>
I'm using YUI 2.7.0 and Struts for my project and have a form with two submit buttons. One is for Save and the other is for Delete. The form submits to a DispatchAction. I've added the onclick listeners successfully, but the problem is that the form submits before the listeners execute. I need to set the dispatch parameter before the form submits. How do I do this? Below is what I have so far:
<form name="adminUserForm" method="post" action="manage_user.do">
<input type="hidden" id="dispatch" name="dispatch" value="unspecified"/>
<!-- other form fields here -->
<input type="submit" value="Save User" id="saveUser">
<input type="submit" value="Delete User" id="deleteUser">
</form>
<script type="text/javascript">
function confirmDelete(msg)
{
if (confirm(msg))
{
return setDispatchMethod("delete");
}
else
return false;
}
function setDispatchMethod(methodName)
{
dispatchField = document.getElementById("dispatch");
dispatchField.value = methodName
return true;
}
var onClickSaveUser = function (e)
{
return setDispatchMethod('save');
};
var onClickDeleteUser = function (e)
{
return confirmDelete('Are you sure you want to delete the user?');
};
new YAHOO.widget.Button("deleteUser", {onclick: {fn: onClickDeleteUser }});
new YAHOO.widget.Button("saveUser", {onclick: {fn: onClickSaveUser }});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是 Yahoo 的 Button 小部件代码处理单击事件,并在其中以编程方式提交表单。他们的事件处理程序在您的事件处理程序之前被调用,因此表单在您的事件处理程序被调用之前就被提交了。
以下是一些想法(它们都有效):
方法 1
处理 mousedown 事件而不是 click 事件(mousedown 在单击之前触发)。
方法 2
使用 type=link 而不是 type=submit(默认)。他们仅在 type = Submit 时提交表单(我还没有对此进行足够的测试来查看表单是否仍然自行提交 - 您可能必须手动调用 form.submit())
方法 3
在他们的代码中,我注意到他们创建了一个隐藏字段,其中包含单击以提交表单的按钮的名称(仅该按钮)。输入标签必须有一个 name 属性,而你的标签没有,所以你必须添加一个。然后在服务器上,您只需检查该隐藏字段的值(您的值属性)。
无论您使用哪种方法,您都应该使用它来阻止表单提交:
The problem is Yahoo's code for the Button widget handles the click event, and in it, they programatically submit the form. Their event handler is called before yours, so the form is submitted before your event handler even gets called.
Here are some ideas (they all work):
METHOD 1
Handle the mousedown event instead of the click event (mousedown fires before click).
METHOD 2
Use type=link instead of type=submit (default). They only submit the form if the type = submit (I haven't tested this enough to see if the form still submits by itself - you may have to manually call form.submit())
METHOD 3
Looking at their code, I noticed that they create a hidden field with the name of the button that was clicked to submit the form (ONLY that button). The input tag has to have a name attribute, and yours doesn't, so you would have to add one. Then on the server, you just check for that hidden field's value (your value attribute).
No matter which of these methods you use, you should be using this to stop the form from submitting:
这是一个简单的解决方案,
您可以删除隐藏的输入
您的调度请求变量将设置为“保存用户”
,如果单击第一个按钮或单击第二个按钮,
here is an easy solution
you can remove the hidden input
and your dispatch request variable will be set to "Save User" if the first button is clicked
or "Delete User" is the 2nd is clicked
这个问题已经有好几年了,但我仍然有一个使用 YUI 2.9.0 的项目,我刚刚遇到了同样的问题,所以我将分享我的解决方案,以防其他人发现自己陷入同样的困境。
关于这个问题有几点值得注意。首先,Gabriel 描述的问题是,YUI 2.9.0 按钮单击处理程序在调用单击处理程序之前提交表单,这在 IE 上不是问题,至少在 IE9 上是这样。在 FireFox 上(至少从 FF12 开始)会出现此问题。我怀疑这在 Chrome、Safari 和大多数(如果不是所有)其他浏览器中也会出现问题,而且我在 YUI 按钮单击处理程序中看到了 IE 的特殊情况。
另外,我不喜欢加布里埃尔建议的任何解决方案。使用链接按钮会使我应用于提交按钮的所有自定义样式无效,使用 mousedown 处理程序会丢失一些单击的用户界面语义,并且由于某种原因,YUI 按钮丢失了我为提交按钮指定的原始名称或者,至少,它没有显示在服务器端的 POSTed 值中。然而,本着 Gabriel 链接按钮的精神,我用默认情况下不提交的输入按钮替换了提交按钮,然后添加了一个隐藏输入并自己提交了表单:
最后,由于 YUI 是在单击时提交表单,所以又一个解决方案应该只是捕获表单的提交事件并检查事件目标是否是您的按钮,执行您需要执行的操作,然后根据需要让提交进行或停止。
This question is several years old, but I still have a project using YUI 2.9.0 and I just ran into the same issue, so I'll share my solution in case someone else finds themselves in the same jam.
Several things worth noting about this question. First, the problem Gabriel describes, where the YUI 2.9.0 Button click handler submits the form before your click handler gets called is not a problem on IE, at least through IE9. On FireFox through at least FF12, the problem occurs. I suspect it would also be a problem in Chrome, Safari, and most if not all other browsers as well as I see what appears to be a special case for IE in the YUI Button click handler.
Also, I didn't like any of the solutions Gabriel suggested. Using a link button invalidated all the custom styling I had applied to my submit button, using the mousedown handler loses some of the user interface semantics of a click, and for some reason YUI Button was losing track of the original name I gave the submit button or, at least, it wasn't showing up in the POSTed values on the server side. In the spirit of Gabriel's link button, however, I replaced the submit button with an input button which does not submit by default, then added a hidden input and submitted the form myself:
Finally, since YUI is submitting the form on click, yet another solution should be just to catch the form's submit event and check if the event target is your button, do what you need to do, then let the submit go or stop it, as needed.