【问题标题】:Form Get function without a form没有表单的获取函数
【发布时间】:2014-03-15 04:01:08
【问题描述】:
当按下按钮而不使用表单时,有没有办法使用类似于 html 表单“get”函数的函数来根据文本框的文本附加 url?
<form method="get" action="https://www.google.co.uk/">
<input type="text" name="q"/>
<input type="submit" value="Search">
</form>
我认为我不能为此使用普通表单,因为我希望有 1 个文本框,但有两个功能和两个按钮(Google 搜索和 Bing 搜索)。如果我错了,请随时告诉我如何在一个表单中添加两个功能!
【问题讨论】:
标签:
javascript
html
forms
web
【解决方案1】:
您可以使用常规按钮而不是提交按钮,并在提交前连接它们以更改表单的操作:
<form method="get" action="https://www.google.co.uk/">
<input type="text" name="q"/>
<input type="button" value="Google Search" onClick="this.form.action='https://www.google.co.uk'; this.form.submit();"/>
<input type="button" value="Bing Search" onClick="this.form.action='https://www.bing.co.uk'; this.form.submit();"/>
</form>
【解决方案2】:
这样的事情可能会让你朝着正确的方向前进?
<!DOCTYPE html>
<html>
<body>
<h1>Hello Plunker!</h1>
<input type="text" value="Value of First Text Box" id="box1">
<input type="text" value="Value of Second Text Box" id="box2">
<script>
(function() {
var x = document.getElementById('box1').value;
var y = document.getElementById('box2').value;
alert(document.location +'?'+ x + '?' + y);
})();
</script>
</body>
</html>
http://plnkr.co/edit/ZEso7gtHyCW241O0E59w?p=preview
但理想情况下,您应该查看问题评论中发布的答案。