jQuery提交表单,点击事件和新动作

时间:2013-08-31 05:29:34

标签: jquery forms submit form-submit

我有这样的表格

    <form name="test" action="test/test_new" />
<input type="text" />
<input type="submit" value="search"/>
<input type="button" value="download"/>

我甚至需要点击下载按钮。例如。如果我点击下载,则应提交为text/text_new.xls而不是text/text_new。我怎么能这样做。

3 个答案:

答案 0 :(得分:27)

将id添加到按钮并尝试此

$('#downloadButton').click(function(){
  $('form[name=test]').attr('action','test/test_new.xls');
  $('form[name=test]').submit();
});

答案 1 :(得分:0)

<强> HTML

<input type="button" value="download" id="dwnld"/>

<强> JS

$('#dwnld').click(function(){
var form = $(this).closest('form');
form.attr('action',form.attr('action')+'.xls').trigger('submit');
});

<强> HTML

<form name="test" action="test/test_new" id="formID"/>
<input type="button" value="download" id="dwnld"/>

<强> JS

$('#dwnld').click(function(){
form=$('#formID')
form.attr('action',form.attr('action')+'.xls').trigger('submit');
});

答案 2 :(得分:0)

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

$(document).on('click', 'input.submit', function () {
    var form = $('#form-id');
    var action = $(this).data('action');
    form.attr('action', action);
    form.submit();
});