How can I implement a JavaScript that submit this form according to the clicket button? [duplicate]

为君一笑 提交于 2019-12-25 19:39:12

问题


I have the following problem that I can't solve:

I have this form into an HTML table:

<th width = "8.33%">
    <form id="actionButton" action="salwf.do?serv=1" method="post">
        <button name="accept" value="Accept" type="submit" class="acceptButton">ACCEPT ICON BUTTON</button>
        <button name="cancel" value="Cancel" type="submit" class="cancelButton">CANCEL ICON BUTTON</button>
        <button name="sap" value="SAP" type="submit" class="sapButton">SAP ICON BUTTON</button>
        <input id="testId" name="test" type="hidden">
    </form>
</th>

As you can see this form contain 3 JQuery button element and an hidden input tag.

I need to create a Javascript that do the following operations: when the user click on one of the previous 3 buttons it take the value of the clicked button (Accept or Cancel or SAP) and submit it (in a POST request).

Can I do something like this using JavaScript? How can I implement it?


回答1:


I need to create a Javascript that do the following operations: when the user click on one of the previous 3 buttons it take the value of the clicked button (Accept or Cancel or SAP) and submit it (in a POST request).

The value is submiited along with the request by itself. You have different names, so check the presence of that name while processing your form request in post parameters.

Accept:Accept
Cancel:Cancel
SAP:SAP

Alternatively, what you can do is, to give your buttons different id but same name.

<button id="accept" name="ctrl" value="Accept" type="submit" class="acceptButton">ACCEPT</button>
<button id="cancel" name="ctrl" value="Cancel" type="submit" class="cancelButton">CANCEL</button>
<button id="sap" name="ctrl" value="SAP" type="submit" class="sapButton">SAP</button>

Then you can check the form request which will contain your value with the same key:

ctrl:Accept
ctrl:Cancel
ctrl:SAP

If you have different names, you will have a different key for every button.



来源:https://stackoverflow.com/questions/27231254/how-can-i-implement-a-javascript-that-submit-this-form-according-to-the-clicket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!