简体   繁体   中英

dynamically submit form based on dropdown value

I want to submit form(or setting value for action) with following details

  1. if 'cash' is selected then go to a.php
  2. else go to b.php clicking on submit button.
<tr>  
    <td>
        <label for="payment">Payment Mode:</label>
    </td>  
    <td>
        <select id="payment" name="payment" required="required">  
            <option value="">Select one...</option>  
            <option value="Cash">Cash</option>
            <option value="Cheque">Cheque</option>  `enter code here`
            <option value="Draft">Draft</option>                                                                   
        </select>
    </td>
</tr>

我认为最佳做法是始终在提交时转到相同的php文件,并检查if($_POST["payment"] == "cash")

Here is a code snippet doing what you want:

$("#payment").on("change", function() {
    if($(":selected", this).val() === "Cash") {
        $("#YourFormId").attr("action", "a.php");
    };
    else {
        $("#YourFormId").attr("action", "b.php");
    }
    $("#YourFormId").submit();
});

That will attach a change event handler to your select , check the value of the selected option and setting the correct action of your form according to your conditions

But you need to change the form selector for targetting the form you want to submit.

And a working jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM