我正在创建一个表单,我必须使用jQuery提交它,而我正在使用jQuery v1.12.4。我正在处理的Web应用程序正在使用除jQuery之外的许多其他脚本。以下是我的html代码
<form enctype="multipart/form-data" id="report-form" method="post">
<!-- date range picker with start date and end -->
<div class="input-group date-picker input-daterange" data-date="2012-12-21T15:25:00Z" data-date-format="yyyy-mm-dd">
<input type="text" class="form-control" name="startDate" id="from" value="2019-09-30" readonly="">
<span class="input-group-addon"> to </span>
<input type="text" class="form-control" name="endDate" id="to" value="2019-09-30" readonly="">
</div>
<!-- first button to submit form for first scenario -->
<a href="javascript:void(0);" class="singleReport" data-type="html">
<i class="fa fa-eye"></i>
</a>
<!-- second button to submit form for second scenario -->
<a href="javascript:void(0);" class="singleReport" data-type="pdf">
<i class="fa fa-file-pdf-o"></i>
</a>
<!-- third button to submit form for third scenario -->
<a href="javascript:void(0);" class="singleReport" data-type="excel">
<i class="fa fa-file-excel-o"></i>
</a>
</form>
我在表单中还有其他一些输入字段。以下是我的jQuery代码,可通过单击上面代码中的任何链接来提交表单
$(document).on('click','.singleReport', function(e){
e.preventDefault();
var from = $('#from').val();
var to = $('#to').val();
if(from == '') {
alert('Select from date!');
}
else if(to == '') {
alert('Select to date!');
}
else{
if($(this).attr('data-type') == 'html')
{
$('#report-form').attr('action', '/html');
$('#report-form').attr('target', '_blank');
$('#report-form').submit();
}
else if($(this).attr('data-type') == 'pdf')
{
$('#report-form').attr('action', '/pdf');
$('#report-form').attr('target', '_blank');
$('#report-form').submit();
}
else if($(this).attr('data-type') == 'excel')
{
$('#report-form').attr('action', '/excel');
$('#report-form').attr('target', '_blank');
$('#report-form').submit();
}
}
});
此代码在第一次单击任何链接时成功在新选项卡中提交了表单,但是如果我再次单击同一链接或任何其他链接,则此后不会提交。任何建议表示赞赏。
谢谢:)