如何使用 onclick 事件而不是 href 链接到网页?

发布于 2024-11-04 05:51:13 字数 332 浏览 5 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浊酒尽余欢 2024-11-11 05:51:13

这样,它将获取原始链接的 href 并添加 id 为“switch-fighter-text”的元素的值

$('#switch-fighter-search-button-link').click(function(){
  window.location=$(this).attr("href")+$('#switch-fighter-text').val();
  return false;
});

This way it'll take the href of the original link and add the value of the element with id "switch-fighter-text"

$('#switch-fighter-search-button-link').click(function(){
  window.location=$(this).attr("href")+$('#switch-fighter-text').val();
  return false;
});
夏末染殇 2024-11-11 05:51:13

不太确定您在这里要做什么,但如果您可以提供更多有用的代码。这是我认为您可能会尝试做的一个示例:

<script type="text/javascript>
$('#submit').click(function(){
    var $link = $('#link1');
    //add to the href
    $link.attr('href', $link.attr('href') + "?id=1");
});

//note that if you want to prevent the link from submitting do like so
$('#link1').click(function(){

    //force redirect to a specific url, adding to the href on the fly
    window.location = $(this).attr('href') + "&user=me";

    return false; //prevents href from changing window.location

});

</script>
<body>
    <input id="submit1" type="Submit" value="Submit"></input>
    <a id="link1" href="somelink/test.html">Link</a>
</body>

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:

<script type="text/javascript>
$('#submit').click(function(){
    var $link = $('#link1');
    //add to the href
    $link.attr('href', $link.attr('href') + "?id=1");
});

//note that if you want to prevent the link from submitting do like so
$('#link1').click(function(){

    //force redirect to a specific url, adding to the href on the fly
    window.location = $(this).attr('href') + "&user=me";

    return false; //prevents href from changing window.location

});

</script>
<body>
    <input id="submit1" type="Submit" value="Submit"></input>
    <a id="link1" href="somelink/test.html">Link</a>
</body>
醉生梦死 2024-11-11 05:51:13

像这样的东西应该有效:

$('$switch-fighter-text').change(function() {
    var link = $('#switch-fighter-search-button-link');
    link.attr('href', link.attr('href') + $(this).val());
});

Something like this should work:

$('$switch-fighter-text').change(function() {
    var link = $('#switch-fighter-search-button-link');
    link.attr('href', link.attr('href') + $(this).val());
});
甜心小果奶 2024-11-11 05:51:13
$('#switch-fighter-search-button-link').attr("href", $('#switch-fighter-search-button-link').attr("href") + $('$switch-fighter-text').val() );

这会将文本框的值添加到已经存在的 href src 中,

但我认为在单击页面后更改 href src 将不起作用。
所以使用

onclick=" window.location='"' + $('#switch-fighter-search-button-link').attr("href") + $('$switch-fighter-text').val(); + '"'; "
$('#switch-fighter-search-button-link').attr("href", $('#switch-fighter-search-button-link').attr("href") + $('$switch-fighter-text').val() );

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

onclick=" window.location='"' + $('#switch-fighter-search-button-link').attr("href") + $('$switch-fighter-text').val(); + '"'; "
江心雾 2024-11-11 05:51:13
$('#switch-fighter-search-button-link').click(function(){
   window.location="/fighters/search/"+$('$switch-fighter-text').val();
   return false;
});
$('#switch-fighter-search-button-link').click(function(){
   window.location="/fighters/search/"+$('$switch-fighter-text').val();
   return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文