根据提交按钮单击上的选择字段选项更改表单操作

时间:2018-02-11 16:35:09

标签: javascript php jquery html forms

我有一个表单,我想根据<select>标签中选择的选项重定向到我网站上的特定页面。例如,如果我选择2checkout,它应该转到gateways / 2checkout / 2checkout.php,同样如果选择了Payza,那么它应该转到gateways / payza / payza.php,依此类推。这应该在点击提交按钮时发生。我怎样才能做到这一点?

示例表单:

<form method="post" action="gateways/<gateway_folder>/<gateway_file.php>">
    <tr>
        <th style="width: 30%">Amount</th>
            <td>
                <span style="display: flex"><span class="currency">$</span>
                <input type="text" id="amount" required class="form-control" placeholder="0.00" style="max-width: 100px" onkeypress="return isNumber(event)"></span>
           </td>
       </tr>
       <tr>
           <th>Payment Gateway</th>
           <td>
               <select name="gateway" class="form-control col-sm-3">
                   <option value="2checkout">2Checkout</option>
                   <option value="payza">Payza</option>
                   <option value="paypal">Paypal</option>
               </select>
           </td>
       </tr>
       <tr>
           <th></th>
           <td><input type="submit" value="Recharge" id="submit" class="btn btn-primary"></td>
       </tr>
       <tr>
           <td colspan="2">Note: Any currency conversion is done as per updated currency rate using Google Currency Conversion API.</td>
       </tr>
</form>

2 个答案:

答案 0 :(得分:0)

试试这个;

$("[name=gateway]").change(function(){

    var val = $(this).val();
    $("form").attr("action","gateways/"+val+"/"+val+".php");
})

答案 1 :(得分:0)

您可以等到用户提交表单然后从select中读取值,这样您就不需要一遍又一遍地运行jQuery代码:

$('form').submit(function(){
  let action = $('[name=gateway]').val();
  $(this).attr('action', 'gateways/' + action  + '/' + action + '.php');
});