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>