[英]Appending form input value to action url as path
我有一個這樣的表格:
<form action="http://localhost/test">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>
如果我輸入一個值,讓我們說:在文本輸入字段中輸入“hello”並提交表單,URL 看起來像: http://localhost/test/?keywords=hello
。
我希望將值附加到操作路徑。 所以基本上,在表單提交后,URL 應該如下所示:
http://localhost/test/hello
您可以使用onsubmit
屬性並在函數內設置操作,例如:
<form id="your_form" onsubmit="yourFunction()">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>
function yourFunction(){
var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}
您可以使用onsubmit
並使用jQuery
來獲得相同的結果。
檢查以下內容。
HTML代碼:
<form id = "your_form" onsubmit="yourFunction()">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>
jQuery代碼:
function yourFunction(){
var action_src = $("keywords").val();
var your_form = $('your_form').val();
var urlLink = "http://localhost/test/";
urlLink = urlLink + action_src;
your_form.action = urlLink;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.