在sumbit格式之前将文本添加到参数 [英] Add text to parameter before sumbit form

查看:64
本文介绍了在sumbit格式之前将文本添加到参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表格

<form action="http://example.com/search" method="get">
    <input type="text" name="q">
    <input type="submit">
</form>

当我用一些文本(例如'AAAAA')填充参数q并提交此表单时,URL变为 http://example.com/search?q=AAAAA .

When I fill parameter q with some text (e.g. 'AAAAA') and submit this form, the url become to http://example.com/search?q=AAAAA.

但是,我想在提交之前将一些文本与其他文本一起添加到参数q中.例如,如果用户输入"AAAAA",则参数将变为"AAAAA BBBBB CCCCC".因此,该网址将变为 http://example.com/search?q=AAAAA+BBBBB+ CCCCC .

But, I want add some text to parameter q with other text before submit. For example, if user input 'AAAAA' the parameter become 'AAAAA BBBBB CCCCC'. So the url become to http://example.com/search?q=AAAAA+BBBBB+CCCCC.

推荐答案

在提交之前使用JavaScript修改值.将onsubmit事件添加到表单中,当您提交按钮时将触发该事件.这样...

Use JavaScript to modify the value before submit. Add an onsubmit event to the form which will get fired when you submit the button. Like this...

<form action="http://example.com/search" method="get" onsubmit="return addText();">
    <input type="text" name="q" id="q">
    <input type="submit">
</form>

<script>
function addText(){
    document.getElementById("q").value += " BBBB CCCC"; // Whatever your value is
    return true;
}
</script>

这篇关于在sumbit格式之前将文本添加到参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆