要在form表单提交时添加参数,可以使用隐藏字段(hidden field)或JavaScript来实现。

使用隐藏字段:

  1. 在form标签中添加一个input标签,type属性设为hidden。
  2. 在input标签中设置name和value属性,分别表示参数名和参数值。

示例代码:

<form action='example.php' method='POST'>
    <input type='hidden' name='param1' value='value1'>
    <input type='text' name='username'>
    <input type='submit' value='Submit'>
</form>

在这个例子中,当用户点击提交按钮时,form表单会向example.php发送POST请求,同时传递了两个参数:param1的值为value1,username的值为用户输入的内容。

使用JavaScript:

  1. 给form表单添加一个onsubmit事件。
  2. 在事件处理函数中,使用JavaScript修改form表单的action属性,添加参数并赋值。

示例代码:

<form action='example.php' method='POST' onsubmit='addParam()'>
    <input type='text' name='username'>
    <input type='submit' value='Submit'>
</form>

<script>
    function addParam() {
        var form = document.forms[0];
        form.action += '?param1=value1';
    }
</script>

在这个例子中,当用户点击提交按钮时,form表单会向example.php发送POST请求,同时传递了一个参数:param1的值为value1。

如何在form表单提交时添加参数

原文地址: https://www.cveoy.top/t/topic/lLTV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录