Change HTML Post parameter on Submit

2019-07-20 02:55发布

I have 1 form, with 2 Submit buttons.

When I click one button, I want certain values to be posted. When I click other button I want other values to be posted.

This is what I tried, did not work:

<form action="/test" method="post">

    <input type="hidden" id="bool" name="bool" value="" />

    <input type="submit" value="Yes" onclick="test()" />
    <input type="submit" value="No" onclick="test1()" />

</form>

<script type="text/javascript">
    function test() {
        document.getElementById('bool').value = "true";
    }
    function test1() {
        document.getElementById('bool').value = "false";
    }
</script>

2条回答
做自己的国王
2楼-- · 2019-07-20 03:04

Use this instead:

<form action="/test" method="post" onsubmit="document.getElementById('bool').value = 'true'; return true;">

    <input type="hidden" id="bool" name="bool" value="" />

    <input type="submit" value="Yes" onclick="test()" />
    <input type="submit" value="No" onclick="test1()" />

</form>
查看更多
在下西门庆
3楼-- · 2019-07-20 03:10
<form action="/test" method="post" name="myform">

    <input type="hidden" id="bool" name="bool" value="" />

    <input type="button" value="Yes" onclick="test()" />
    <input type="button" value="No" onclick="test1()" />

</form>

<script type="text/javascript">
    function test() {
        document.getElementById('bool').value = "true";
        document.myform.submit();
    }
    function test1() {
        document.getElementById('bool').value = "false";
        document.myform.submit();
    }
</script>
查看更多
登录 后发表回答