我有一个像这样的jQuery函数:
$(function(){
function unshareDialog(event) {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:200,
width: 365,
modal: true,
buttons: {
"Unshare": function() {
$('#unshare_form').submit();
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
return false;
}
$('#unshare_entire_file').click(unshareDialog);
});
我的表单有两个提交按钮,如下所示:
<form action="/unshare/" id="unshare_form" method="post">
<input type="submit" value="Unshare" name="unshare_to_user" id="unshare_to_user" />
<input type="submit" value="Unshare" id="unshare_entire_file"
name="unshare_entire_file" />
</form>
在服务器中,我这样做:
if request.POST.get('unshare_entire_file'):
...unshare for file
else
..unshare for user
但事实证明,当我使用jQuery
提交表单时,这不起作用。我想知道单击了哪个提交按钮并进行相应处理。我怎么能这样做?
答案 0 :(得分:0)
这样的事情怎么样?
$(function () {
function unshareDialog(event, elem) {
event.preventDefault();
var input = $("<input>").attr("type", "hidden").attr("name", elem.attr("id")).val(1);
$("#unshare_form").prepend(input);
$("#dialog-confirm").dialog({
resizable: false,
height: 200,
width: 365,
modal: true,
buttons: {
"Unshare": function () {
$("#unshare_form").submit();
},
Cancel: function () {
$(this).dialog("close");
}
}
});
return false;
}
$('#unshare_entire_file,#unshare_to_user').click(function (e) {
unshareDialog(e, $(this));
});
});
这显然不是最佳的,但应该有效