如何将字符串与变量连接起来/传递到链接

How do you concatenate a string with a variable/pass to link?

本文关键字:链接 起来 连接 字符串 变量      更新时间:2024-05-18

我正在尝试制作一个简单的页面,将IR代码发送到手机(OneRemoteToControlThemAll)上的应用程序。这就是app的dev通过html与它通信的方式,它可以100%正常工作。

>"Send codes using URI "otrta://code?id=xxx" or "otrta://script?id=xxx" - use it for HTML layouts!"

<button type="button"><a href="otrta://code?id=12372">Left</a></button>

但是,这只适用于pre-entered id。所以,我想要一个带有按钮的文本框,当点击按钮时,它会发送在框中输入的代码。我四处寻找不同的方法,尝试了很多,但都不太奏效。这是我最近的一次尝试:

<script type="text/javascript">
  function myfunction()
  {
    var code = "otrta://code?id=" + document.getElementById("textbox1").value;
    return code;
  }
</script>

html:

<input type="text" name="textbox1" Id="textbox1" style="width: 194px"/>
<button type="button" id="submit"><a href="javascript:myfunction();">Submit</a></button>

现在,在我电脑上的chrome上,这将带我进入一个页面,并输出otrta://code?id=1234或我输入的任何数字。在我的手机上,这个按钮什么也没用。有什么解决方案可以让它和其他人一样工作?它不需要是完美的形式,只是一些会起作用的东西,谢谢你的帮助。

您的返回值将被丢弃。您需要设置window.locationhref属性。

 <script type="text/javascript">
 function set_href() {
   var code = "otrta://code?id=" + document.getElementById("textbox1").value;
   window.location.href = code;
 }
 </script>

--

<input type='submit' onclick='set_href()'>

尝试替换href本身:

function myfunction(link) {
  link.href = "otrta://code?id=" + document.getElementById("textbox1").value;
}
<input type="text" name="textbox1" Id="textbox1" style="width: 194px" />
<button type="button" id="submit"><a href="#" onclick='myfunction(this);'>Submit</a>
</button>