如何使用 onclick 事件而不是 href 链接到网页?
我有一个带有 href 的锚标记。我需要在输入文本框中的 href 中的最后一个 / 之后添加一个字符串。我尝试将输入框的值添加到 href 但没有成功。我可以使用 onclick 事件将值添加到链接字符串吗?如何使用 jquery 来实现这一点?这是代码:
//This is the Search Button
$('#switch-fighter-search-button-link').attr("href","/fighters/search/");
//This is the Input box
var sft = $('$switch-fighter-text').val();
I have an anchor tag that has an href. I need to add a string after the last / in the href from an input text box. I have tried to add the value of the input box to the href with no success. Can I add the value to the link string using the onclick event? How can this get accomplish using jquery? Here is the code:
//This is the Search Button
$('#switch-fighter-search-button-link').attr("href","/fighters/search/");
//This is the Input box
var sft = $('$switch-fighter-text').val();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样,它将获取原始链接的 href 并添加 id 为“switch-fighter-text”的元素的值
This way it'll take the href of the original link and add the value of the element with id "switch-fighter-text"
不太确定您在这里要做什么,但如果您可以提供更多有用的代码。这是我认为您可能会尝试做的一个示例:
Not exactly sure what you're trying to do here but if you could provide more code that would be useful. Here's an example of what I think you might be trying to do:
像这样的东西应该有效:
Something like this should work:
这会将文本框的值添加到已经存在的 href src 中,
但我认为在单击页面后更改 href src 将不起作用。
所以使用
This will add your textbox's value to the already existing href src
but i think that can change the href src after you clicked on the page will not work.
So use