将输入字符串附加到锚标记href
Append input string to anchor tag href
尝试使用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>
但是,不起作用
加载页面时,您触发searchProduct()
一次。您需要在ID为inputId
的输入更新后触发此操作——无论是通过按下按钮还是当他们离开文本框时,等等。
以下是一个在他们离开文本框时触发searchProduct()
功能的示例:
(function() {
function searchProduct(){
var inputId = document.getElementById('inputId').value;
document.getElementById('go').href = "mywebsite/product/" + inputId;
}
document.getElementById('inputId').addEventListener('blur', function() {
searchProduct()
});
})();
请参阅此jsBin
我希望这就是你想要的。
function searchProduct(){
var inputValue = document.getElementById('inputId').value;
document.getElementById('go').href = "mywebsite/product/"+inputValue;
}
<input id="inputId" type="text">
<a id="go" onclick="searchProduct();" href="">anchor tag</a>
您应该在自身之外执行函数,而不是在内部
function searchProduct(){
var inputId = document.getElementById('inputId').val();
document.getElementById('go').href = "mywebsite/product/" + inputId;
}
searchProduct(); // This should be executed via an eventListener to the input
相关文章:
- Href到已检查的输入收音机
- 名称href标签根据用户文本输入JavaScript
- 使用php使用href链接将输入的文本框值传递到另一个页面
- 使用Href Javascript调用更改隐藏输入
- 将输入字符串附加到锚标记href
- 页面设计<a href><按钮><输入>在JSP中
- 如何从一个 HREF 填充多个输入字段
- 输入框上的 Href 不起作用
- 使用 href Jquery 的值更新输入
- 将输入字段设置为包含 window.location.href
- 根据表单输入更改href属性的值
- 使文件输入成为一个href
- 使用文本输入值动态更改链接href属性
- jquery-使用href按钮删除特定输入
- 创建regex以将输入值中的href替换为单引号
- 使用javascript将表单输入值插入href目标URL
- 获取href附近的输入值
- jQuery函数使用href代替输入
- 从href click输入文本
- 如何在href中传递输入的值?