使用oninput向输入发送val的问题

时间:2014-04-12 22:45:51

标签: javascript jquery html css

Fiddle Demo

这里是JSfiddle,它似乎无法使用其他输入的val更新只读输入(它将成为隐藏输入)

HTML

<form>
    <input id="otherAmt" class="qty" type="text" placeholder="Amount Hidden" />
    <p> The Costs are:</p> <input type="text" id="otherAmtHidden" name="otherAmtHidden" readonly="" />
</form>

var otherAmt = document.getElementById("otherAmt");

var otherAmtHidden = document.getElementById("otherAmtHidden");

otherAmt.oninput =  function(){
   alert('input Received');
   var val1 = parseInt(otherAmt.value, 10);
   alert(val1 + 'val1');
   var amtHidden = val1;
   alert(amtHidden + 'amtHidden');
   //otherAmtHidden.innerHTML = amtHidden;
   $('input [name="otherAmtHidden"]').val(amtHidden);
   alert('finished');
};

修改:我很笨,通过将$('input etc').val(amtHidden);更改为$('#otherAmtHidden).val(amtHidden);

解决了问题

1 个答案:

答案 0 :(得分:0)

我很笨,我通过改变$('输入等')来解决它.val(amtHidden); to $('#otherAmtHidden).val(amtHidden);

Fixed Javascript

var otherAmt = document.getElementById("otherAmt");
var otherAmtHidden = document.getElementById("otherAmtHidden");

otherAmt.oninput =  function(){
   var val1 = parseInt(otherAmt.value, 10);
   var amtHidden = val1;
   $('#otherAmtHidden').val(amtHidden);
};