我如何将在输入文本字段中键入的URL附加到锚点,然后在单击锚点时跟随它? [英] How do I append a url typed into a input text field to an anchor then follow it when anchor is clicked?

查看:84
本文介绍了我如何将在输入文本字段中键入的URL附加到锚点,然后在单击锚点时跟随它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个文本字段,人们可以在其中输入值.然后,我想用href打开一个URL,并将文本字段附加到末尾.

I want to have a text field where people can type in a value. Then I want to have a href open a url with the text field appended to the end.

因此,如果文本字段显示"elephant",则当他们单击链接时,将在example.com/elephant上打开一个页面.

So if the text field says "elephant" then when they click the link, a page will open with at example.com/elephant.

我认为javascript是实现这一目标的最简单方法,但我只是不知道怎么做.

I think javascript would be the easiest way to accomplish this but I just don't know how.

推荐答案

我假设您具有类似以下内容的HTML,并希望在锚点时将appendUrl输入字段的内容附加到锚点标记中的url标签被点击.

I'm assuming that you have some HTML like the following and want to append the contents of the appendUrl input field to the url in the anchor tag when the anchor tag is clicked.

<a href="/base/url" id="baseUrl">Link Text</a>
<input type="text" id="appendUrl" />

然后,使用jQuery,它看起来像:

Then, using jQuery, it would look something like:

$(function() {
    $('#baseUrl').click( function() {
        window.location = $(this).attr('href') + '/' + $('#appendUrl').val();
        return false;
    });
});

基本思想是提取输入的值并将其附加到href中的url中.使用如此构造的值来设置当前窗口的位置.我在点击处理程序中返回false,以防止采取默认操作(仅基本网址).

The basic idea is to extract the value of the input and append it to the url in the href. Use the value so constructed to set the location of the current window. I return false in the click handler to prevent the default action (the base url alone) from being taken.

这篇关于我如何将在输入文本字段中键入的URL附加到锚点,然后在单击锚点时跟随它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆