【问题标题】:How to create a link incorporating user input from the input box如何创建包含来自输​​入框的用户输入的链接
【发布时间】:2020-01-09 11:43:48
【问题描述】:

我希望与一个网络系统交互,其中包含由唯一代码定义的文章。

例如订单系统包含订单ORD001、ORD002

订单系统包含一个网站,每个订单都有自己独特的页面。

查看ORD001 的公共网址将是www.sampleorders.com/ORD001ORD002 将是www.sampleorders.com/ORD002

我想阻止用户使用输入框,他们可以在其中输入订单号,并被路由到正确的网址。

有没有使用 html 和 javascript 连接预定义字符串 (www.sampleorders.com/) 和表单用户输入 (ORD001) 的简单方法?

【问题讨论】:

标签: html webforms


【解决方案1】:

您可以为此使用 javascript 和/或 jquery。

尝试在下面的 sn-p 中传递此帖子 ID(在 url 中)。

$("input#post_id").on("change", function(){ // Whenever this field changes

  var post_id = $("input#post_id").val(); // Get the value of the field
  
  $("a#link").attr("href", "https://stackoverflow.com/questions/" + post_id); // Update the href in the link to match the id inserted in the input field
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label>Which post would you load ?</label>

<br>

<input id="post_id" type="text">

<a id="link" href="#">Go</a>

【讨论】:

    猜你喜欢
    • 2015-11-10
    • 2014-01-26
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多