将 javascript 变量作为 onsubmit href 链接传递到表单/输入字段
id 喜欢有一个输入框,用户可以在其中输入搜索词,该搜索词可能会传递给一个 javascript 函数,然后该函数将一些 url 段与搜索词组合起来,创建一个完整的 url。到目前为止,它在没有表单的情况下工作正常,但我想向其中添加一个表单,以便用户只需按 Enter 键即可,而不是单击提交按钮。
提交按钮代码:
<a href="javascript: void(0)" onClick="this.href = i1 + document.getElementById('intranet').value + i3" target="_blank">
<div id="submit" class="out"
onMouseOver="document.getElementById('submit').className = 'over';"
onMouseOut="document.getElementById('submit').className = 'out';">
<span>Submit</span>
</div>
</a>
错误的表单/输入代码:
<form action="" onSubmit="this.href = i1 + document.getElementById('intranet').value + i3; return false;" method="get" target="_blank">
<input type="text" id="intranet" size="15" value="Search Intranet.."
onFocus="document.getElementById('intranet').value = ''"
onBlur="document.getElementById('intranet').value = 'Search Intranet..'" / >
</form>
可能的js函数创建url:
<script language="javascript" type="text/javascript">
function urlGen(value)
{
var i1 = "seg1";
var i2 = document.getElementById('intranet').value;
var i3 = "seg2";
var fullURL = i1 + document.getElementById('intranet').value + i3;
return fullURL;
}
</script>
id like to have an input box that a user can enter a search term into that gets passed to maybe a javascript function that then combines some url segments with the search term creating a full url. up until now its been working fine without a form around it, but i would like to add a form to it so users can just hit enter rather than clicking a submit button.
submit button code:
<a href="javascript: void(0)" onClick="this.href = i1 + document.getElementById('intranet').value + i3" target="_blank">
<div id="submit" class="out"
onMouseOver="document.getElementById('submit').className = 'over';"
onMouseOut="document.getElementById('submit').className = 'out';">
<span>Submit</span>
</div>
</a>
faulty form/input code:
<form action="" onSubmit="this.href = i1 + document.getElementById('intranet').value + i3; return false;" method="get" target="_blank">
<input type="text" id="intranet" size="15" value="Search Intranet.."
onFocus="document.getElementById('intranet').value = ''"
onBlur="document.getElementById('intranet').value = 'Search Intranet..'" / >
</form>
possible js function to create url:
<script language="javascript" type="text/javascript">
function urlGen(value)
{
var i1 = "seg1";
var i2 = document.getElementById('intranet').value;
var i3 = "seg2";
var fullURL = i1 + document.getElementById('intranet').value + i3;
return fullURL;
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的:
Maybe something like:
使用表单的 onSubmit 事件将适当的数据设置为 Intranet 的输入 - 不要更改 this.href (表单没有此类属性)。请记住,JavaScript 可以在客户端禁用。还可以考虑将隐藏输入放入 i1 和 i2 的表单中,并在服务器端执行所有组合逻辑。
Use form's onSubmit event where set the appropriate data to the intranet's input - don't change this.href (form does not have such attribute). Remember though that JavaScript can be disabled at the client's side. Consider also placing hidden inputs into the form for i1 and i2 and do all combination logics at the server side.
还应该注意,这是一个侧边栏小工具,只需要与 ie 一起使用,并且服务器/客户端限制为空。尝试了这两种方法(我认为 - 仍然是初学者),但是当我按回车键时发生的所有事情都是页面重新加载(target =“_blank”打开相同的新页面)。
我喜欢完全取消提交按钮区域,但它可以在没有表单标签的情况下发挥作用。
我现在拥有的:
should also note that this is for a sidebar gadget and just needs to work with ie and server/client limitations is null. tried both of those methods (i think - still a beginner), but all that happens when i hit enter is the page reloads (target="_blank" opens new page of the same).
id like to do away with the submit button area entirely, but it is functional with that and without the form tags.
what i have right now: