Javascript 将输入标记值传递给as href参数

Javascript 将输入标记值传递给as href参数,javascript,html,Javascript,Html,我想在href中将输入标记的值作为参数(quantita)传递。 我应该用javascript来做这个吗?对不起,我的英语太差了 谢谢 为您的设置id或类,例如: 因此,您可以像这样使用jQuery: jQuery(document).ready(function(){ jQuery('qta_field').change(function(){ var tmpVal = jQuery('#qta_field').val(); var tmphref

我想在href中将输入标记的值作为参数(quantita)传递。 我应该用javascript来做这个吗?对不起,我的英语太差了 谢谢



为您的
设置id或类,例如:

因此,您可以像这样使用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);
    });
});

要将数据从输入发送到服务器,应使用表单

<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>

更新

您可以使用document.queryselector来定位并使用js操作href属性

例如:

input=document.querySelector('input');
a=document.querySelector('a');
a、 setAttribute('href',a.getAttribute('href')+input.value)

使用链接而不使用任何库的最简单方法:

<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>


为什么不能直接使用
&quantita=${item.value}
?您想在提交表单时传递值?当您想传递此值时?item.value包含一个用户可以更改的值。我插入一个图像作为示例,在您的代码中,“更新”链接已消失。它不可单击更新:添加href=''到它可以工作,但如果我有一个“更新链接”列表,它只删除第一个不理解的,请在jsp(购物车)中给我示例我有一份文章清单。我用foreach标记扫描列表,用你的代码我只能更新列表顶部的第一篇文章。从第二篇到最后一篇的文章不会更新
<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>