jQuery 提交带有新操作的单击事件的表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18544854/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:54:25 来源:igfitidea点击:
jQuery submit form with click event with new action
提问by overflow
I am having a form like this
我有这样的表格
<form name="test" action="test/test_new" />
<input type="text" />
<input type="submit" value="search"/>
<input type="button" value="download"/>
I need a click even for download button. Eg. If i click download it should submit as text/text_new.xls
instead of text/text_new
. How can i do it.
我什至需要点击下载按钮。例如。如果我点击下载它应该提交text/text_new.xls
而不是text/text_new
. 我该怎么做。
回答by commit
Give id to button and try this
给按钮 id 并尝试这个
$('#downloadButton').click(function(){
$('form[name=test]').attr('action','test/test_new.xls');
$('form[name=test]').submit();
});
回答by Tushar Gupta - curioustushar
html
html
<input type="button" value="download" id="dwnld"/>
js
js
$('#dwnld').click(function(){
var form = $(this).closest('form');
form.attr('action',form.attr('action')+'.xls').trigger('submit');
});
or
或者
HTML
HTML
<form name="test" action="test/test_new" id="formID"/>
<input type="button" value="download" id="dwnld"/>
js
js
$('#dwnld').click(function(){
form=$('#formID')
form.attr('action',form.attr('action')+'.xls').trigger('submit');
});
回答by Veritas_UA
HTML
HTML
<form name="test" id="form-id" action="test/test_new" />
<input type="text" />
<input type="button" class="submit" value="search" data-action="test/test_new" />
<input type="button" class="submit" value="download" data-action="test/test_new.xls" />
<input type="button" class="submit" value="another" data-action="test/test_newer" />
JS
JS
$(document).on('click', 'input.submit', function () {
var form = $('#form-id');
var action = $(this).data('action');
form.attr('action', action);
form.submit();
});