我有一个JavaScript,可以将输入按钮的属性更改为“已禁用”,因此用户不会提交两次页面。
// Disables multiple submit
function disableSubmit (btn, submitForm) {
// Sets button as disabled, changes class and cursor
$(btn).attr("disabled", true);
$(btn).toggleClass("disabled alt2");
$(btn).css('cursor', 'initial');
$(btn).attr("value", "Sender...");
// Submits form
$(submitForm).submit();
}
// Disables submit for order form
$("#send-order").click(function () {
return disableSubmit(this, "#order");
});
问题是我需要跟踪提交页面的按钮的名称,以便我可以在我的Django view.py
文件中处理它。
<input value="Send bestilling" id="foo" name="send-order" type="submit" class="button alt2">
request.POST.get("send-order", False)
但如果我使用我的脚本提交页面,我就无法获得提交页面的输入名称。有没有办法为我的脚本中提交页面的按钮设置名称?
我尝试了这个但是没有用:
$(submitForm).submit().attr("name", "send-order");
我必须更改views.py
中的代码才能使其正常工作。
答案 0 :(得分:0)
你可以使用preventdefault或return false
$("#send-order").click(function (event) {
event.preventDefault();
return disableSubmit(this, "#order");
});
或者
<input id="send-order" value="Send" type="submit" onclick="disableSubmit(this, '#order');return false" />
答案 1 :(得分:0)
这样做的一种方法是在表单上创建一个隐藏的输入,并使用自己的名称。像下面的东西。
<input type="hidden" name="buttonName" id="buttonName" />
然后,在disableSubmit函数中,让它在提交之前设置该输入的值。
$(submitForm)
.find("input[name=buttonName]")
.attr("value",
$(btn).attr("name")
);
从那里,您只需检索View.py中的“buttonName”值。