jQuery jquery更改表单目标值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12590536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery to change form target value
提问by user1684099
I need to make a button's target different from the main submit value. I am using jquery with code below:
我需要使按钮的目标与主提交值不同。我正在使用带有以下代码的 jquery:
HTML:
HTML:
<form action="somepage" id="cartform" target="_parent" method="post">
(So default action is parent)
(所以默认操作是父级)
JQUERY:
查询:
$("#refresh").click(function(){
$("#cartform").attr('target', '_self');
});
("Refresh" is the form's extra button to keep the form in a popup box)
(“刷新”是表单的额外按钮,用于将表单保留在弹出框中)
回答by Giona
Try to add a listener on submit instead of click:
尝试在提交而不是单击时添加侦听器:
$('cartform').on('submit',function(){
$(this).attr('target', '_self');
});
Obv it's the case only if you always want the target to change as the form is submitted. Otherwise, try with your code and preventDefault
.
Obv 仅当您始终希望在提交表单时更改目标时才会出现这种情况。否则,请尝试使用您的代码和preventDefault
.
$("#refresh").click(function(ev){
ev.preventDefault();
$("#cartform").attr('target', '_self').submit();
});
I think the form is being submitted before the target
change applies.
我认为表格是在target
更改适用之前提交的。
Edit
编辑
Actually, OP is trying to submit a form inside an iframe. This should work:
实际上,OP 正在尝试在 iframe 内提交表单。这应该有效:
$("#refresh").click(function(ev){
ev.preventDefault();
$("#cartform").attr('target', 'name_of_the_iframe').submit();
});
Will submit to:
将提交给:
<iframe name="name_of_the_iframe" ...