【发布时间】:2015-02-10 06:24:05
【问题描述】:
尝试使用 vanilla js 将用户输入的字符串附加到锚标签 href。
<script>
function searchProduct(){
var inputId = document.getElementById('inputId').value;
document.getElementById('go').href = "mywebsite/product/" + inputId;
}
searchProduct();
</script>
<a id="go"><p>Go</p></a>
但是,这不起作用
【问题讨论】:
-
在哪里触发函数?
-
请显示锚标签的html
-
好的——还有输入元素。
-
您在页面加载时触发了
searchProduct(),之前有人在inputId框中填写任何内容。你需要触发它,在按钮点击、onblur 或某事上
标签: javascript anchor href