我有两个输入字段,我想将相同的数据发布到两个不同的PHP文件,具体取决于点击的任何按钮.
在我的情况下,数据只进入foo.PHP,但不是进入excel.PHP.
我想要数据去excel.PHP,如果第二个按钮被按下.
JS:
$(function() { $("#from").datepicker({ defaultDate: "+1w",changeMonth: true,numberOfMonths: 3,onClose: function(selectedDate) { $("#to").datepicker("option","minDate",selectedDate); } }); $("#to").datepicker({ defaultDate: "+1w",onClose: function(selectedDate) { $("#from").datepicker("option","maxDate",selectedDate); } }); });
HTML:
<form action="foo.PHP" method="post"> <label for="from">From</label> <input type="text" id="from" name="from" /> <label for="to">to</label> <input type="text" id="to" name="to" /> <br> <input type="submit" value="Viewchart"> </form> <form action="excel.PHP" method="post"> <input type="submit" value="Download Excel file"> </form>
点击按钮时可以更改动作属性,如下所示:
$(function() { $( ".actionable" ).click( function() { $('#myForm').attr('action',$(this).data("action")); $('#myForm').submit(); }); });
并将下一个data- *属性设置为您的按钮:
<form name="form" id="myForm" method="post"> <input type="submit" class="actionable" value="Viewchart" data-action="foo.PHP"/> <input type="submit" class="actionable" value="Excel" data-action="excel.PHP"/> </form>
你可以看看它如何工作here.
相关链接:
>您可以在这里阅读更多关于data- *属性:http://www.w3schools.com/tags/att_global_data.asp
Clarification for OP:
The
$( function() {} );
block —equivalent to$(document).ready( function() {} );
— specify a function to execute when the DOM is fully loaded; you should put inside all your code which interact with the elements of the DOM.