使用SPAN值设置/绑定输入值 [英] Set/bind input values using SPAN values

查看:432
本文介绍了使用SPAN值设置/绑定输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的Code-pen,计算结果显示在输入字段SPAN的旁边.我试图从SPAN获取该计算值并覆盖输入字段.

I have a working Code-pen, the calculated results are showing on next to input fields SPAN. I tried to get that calculated value from SPAN and overwrite the input fields.

 <!-- Include this line of code --><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtx1" /><br />
<span id="txtSpan"></span>
<input type="button" value="Appended-textBox" id="Btn3" />

和JS

 $(document).ready(function() {
      $('#Btn3').click(function() {
        var txtvalue = $('#txtx1').val();
        $("#txtSpan").text(txtvalue);
        console.log(txtvalue);
      });
    });

上面的工作我只是想通过改变SPAN值来设置Input.

有什么方法可以用计算出的SPAN值覆盖输入框,并将最终的SPAN结果写入输入(ID = GrandTotal),

Is there any way I can overwrite the input box with calculated SPAN values and for the final SPAN result to write to input (ID=GrandTotal),

<div>
  <label class="description" for="Coconut">Coconut</label>
  <input id="Coconut" name="Coconut" class="element text medium" type="text" maxlength="255" value="10" readonly="true"/>
</div>


<div>
  <label class="description" for="GrandTotal">Grand Total</label>
  <input id="GrandTotal" name="GrandTotal" class="element text medium" type="text" maxlength="255" value="" readonly="true"/>
</div>

非常感谢,很抱歉浪费您的时间.非常感谢

https://codepen.io/dunya/pen/mojKNz

https://codepen.io/dunya/pen/mojKNz

推荐答案

我设法通过添加以下代码行对其进行了修复:请检查笔上方的工作版本.

I managed to fix it by adding below line:please check the working version above pen.

$('#GrandTotal').val(parseInt($(this).html())); 

当我尝试

$('#GrandTotal').val(sum); 

它给了我错误的计算结果.

it gave me the wrong calculation.

这篇关于使用SPAN值设置/绑定输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆