Adding submit button value to submit()
I am using the Jquery UI dialog box to act as a confirmation when deleting a record. What I'm trying to achieve is that when you click on a submit button with the value "delete" it will open the dialog window and confirm your choice. If you choose yes it will submit the form with the value of the submit button. I understand that it won't work at the moment because submit() has no way of knowing which button was clicked but I'm not sure how to go about it? Many thanks in advance.
<script type="text/javascript">
$(document).ready(function(){
$("#dialog").dialog({
modal: true,
bgiframe: true,
width: 500,
height: 200,
autoOpen: false
});
$("#rusure").click(function(e) {
e.preventDefault();
$("#dialog").dialog('option', 'buttons', {
"Confirm" : function() {
$("#tasks").submit();
},
"Cancel" : function() {
$(this).dialog("close");
}
});
$("#dialog").dialog("open");
});
});
</script>
<input type="submit" name="action" value="Delete" id="rusure"/>
The form is called "tasks" and the hidden div containing the dialog content is called "dialog". At the moment everything is working fine apart fro开发者_开发知识库m the form being submitted with the value of the submit button. There are also 2 submit buttons in the form.
Give this a try:
$("#rusure").click(function(e) {
e.preventDefault();
// Create hidden input from button and append to form
var input = $('<input name="confirm_delete" value="yesplz" type="hidden" />').appendTo('#tasks');
$("#dialog").dialog('option', 'buttons', {
"Confirm" : function() {
$("#tasks").submit();
},
"Cancel" : function() {
$(this).dialog("close");
$(input).remove();// Remove hidden input
}
});
$("#dialog").dialog("open");
});
Demo here: http://jsfiddle.net/Madmartigan/ZGLNc/1/
精彩评论