我有 10 个输入,单击时我需要在另一个输入中输入它们的值,但是 returns [object Object]
I have 10 inputs, I need their values in another input when clicked, but returns [object Object]
var valor = $('.numbers input').each(function(){$('.numbers input').val();});
$(valor).click(function(){
$('input#icms').val(valor);
});
<div class="numbers">
<input type="button" id="number0" value="0">
<input type="button" id="number1" value="1">
<input type="button" id="number2" value="2">
<input type="button" id="number3" value="3">
<input type="button" id="number4" value="4">
<input type="button" id="number5" value="5">
<input type="button" id="number6" value="6">
<input type="button" id="number7" value="7">
<input type="button" id="number8" value="8">
<input type="button" id="number9" value="9">
<input type="button" id="point" value=".">
</div>
<input type="text" class="calc" id="icms" value="" placeholder="CUSTO + ICMS">
我将使用上面的输入来接收来自输入 [type=button] 的值。
Console.log() 显示了所有值,但没有按预期工作
在每个输入类型按钮上附加点击事件。然后使用 this
关键字将单击按钮的值附加到输入。
$('.numbers input').click(function(){
$('input#icms').val($('input#icms').val()+$(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="numbers">
<input type="button" id="number0" value="0">
<input type="button" id="number1" value="1">
<input type="button" id="number2" value="2">
<input type="button" id="number3" value="3">
<input type="button" id="number4" value="4">
<input type="button" id="number5" value="5">
<input type="button" id="number6" value="6">
<input type="button" id="number7" value="7">
<input type="button" id="number8" value="8">
<input type="button" id="number9" value="9">
<input type="button" id="point" value=".">
</div>
<input type="text" class="calc" id="icms" value="" placeholder="CUSTO + ICMS">
您已将 jQuery 对象的输入值更改为 $('selector')
返回的对象。这就是您输入 [Object object]
的原因。另请参阅 $().each(fn)
var valor = $('.numbers input').each(function(){$('.numbers input').val();});
$(valor).click(function(){
$('input#icms').val(valor);
});
<div class="numbers">
<input type="button" id="number0" value="0">
<input type="button" id="number1" value="1">
<input type="button" id="number2" value="2">
<input type="button" id="number3" value="3">
<input type="button" id="number4" value="4">
<input type="button" id="number5" value="5">
<input type="button" id="number6" value="6">
<input type="button" id="number7" value="7">
<input type="button" id="number8" value="8">
<input type="button" id="number9" value="9">
<input type="button" id="point" value=".">
</div>
<input type="text" class="calc" id="icms" value="" placeholder="CUSTO + ICMS">
我将使用上面的输入来接收来自输入 [type=button] 的值。 Console.log() 显示了所有值,但没有按预期工作
在每个输入类型按钮上附加点击事件。然后使用 this
关键字将单击按钮的值附加到输入。
$('.numbers input').click(function(){
$('input#icms').val($('input#icms').val()+$(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="numbers">
<input type="button" id="number0" value="0">
<input type="button" id="number1" value="1">
<input type="button" id="number2" value="2">
<input type="button" id="number3" value="3">
<input type="button" id="number4" value="4">
<input type="button" id="number5" value="5">
<input type="button" id="number6" value="6">
<input type="button" id="number7" value="7">
<input type="button" id="number8" value="8">
<input type="button" id="number9" value="9">
<input type="button" id="point" value=".">
</div>
<input type="text" class="calc" id="icms" value="" placeholder="CUSTO + ICMS">
您已将 jQuery 对象的输入值更改为 $('selector')
返回的对象。这就是您输入 [Object object]
的原因。另请参阅 $().each(fn)