Javascript 将输入标签值作为 href 参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27148673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass input tag value to as href parameter
提问by Alex
I want to pass the value of input tag as parameter(quantita) in href. Should i use javascript to do this?Sorry for my english Thanks
我想在 href 中将输入标签的值作为参数(数量)传递。我应该使用 javascript 来做这件事吗?对不起,我的英语谢谢
<input type="text" id="qta_field" value="${item.value}"/><a href="updateItem?codice=${item.key.codice}&quantita=">update</a>


回答by Simcha
The easiest way to do it with link and without any library:
使用链接而不使用任何库的最简单方法:
<input type="text" id="qta_field" value="${item.value}"/>
<a href='' onclick="this.href='updateItem?codice=${item.key.codice}&quantita='+document.getElementById('qta_field').value">update</a>
回答by Quentin
To send data from an input to the server, you should use a form.
要将数据从输入发送到服务器,您应该使用表单。
<form action="updateItem">
<input id="qta_field" name="quantita" value="${item.value}">
<input type="hidden" name="codice" value="${item.key.codice}">
<button>update</button>
</form>
回答by Buisson
set an id or a class to your <a>for example : <a id='myA'>
为您<a>的示例设置一个 id 或一个类:<a id='myA'>
So you can use jQuery like this :
所以你可以像这样使用jQuery:
jQuery(document).ready(function(){
jQuery('qta_field').change(function(){
var tmpVal = jQuery('#qta_field').val();
var tmphref = jQuery('#myA').attr('href');
tmphref = tmphref+tmpVal;
jQuery('#myA').attr('href',tmphref);
});
});
回答by andrew
You can use the document.queryselector to locate and manipulate the href attribute with js
您可以使用 document.queryselector 来定位和操作带有 js 的 href 属性
Example:
例子:
input = document.querySelector('input');
a = document.querySelector('a');
a.setAttribute('href',a.getAttribute('href')+input.value);
<input type="text" id="qta_field" value="test"/>
<a href="updateItem?codice=${item.key.codice}&quantita=">update</a>

