我想根据输入的焦点在两个不同的字段中显示值。当我将光标放在两个字段中的任何一个并单击按钮时,我只希望按钮的值出现在该字段中。但是我的代码是继续为同一个字段事件增加价值,我有两个不同的字段。有人请帮忙。
<input type="text" class="frm-control-input" name="multi">
<input type="text" class="frm-control-input-price" name="price">
<button type="button" class="btn" value="1">1</button>
<button type="button" class="btn" value="2">2</button>
<button type="button" class="btn" value="3">3</button>
$('.frm-control-input').click(function(){
if($(this).is(':focus')){
console.log('up')
$('.btn').click(function(){
var number = $(this).val();
$('.frm-control-input').val($('.frm-control-input').val() + number);
console.log(number);
});
}else{
$(this).blur();
}
});
$('.frm-control-input-price').click(function(){
if($(this).is(':focus')){
$('.btn').click(function(){
var num = $(this).val();
$('.frm-control-input-price').val($('.frm-control-input-price').val() + num);
});
}
});
您可以data-attribute
在此处使用并将其值更改为true
或false
取决于焦点是哪个输入。然后,每当单击按钮时,检查位置,data-focus="true"
然后在该输入中添加数字。
演示代码:
$('.frm-control-input').click(function() {
//add data-focus true
$(this).attr("data-focus", true);
//remove from other
$(".frm-control-input").not(this).attr("data-focus", false);
});
$('.btn').click(function() {
var number = $(this).val();
//add value to input where data-focus is true
$('input[data-focus=true]').val($('input[data-focus=true]').val() + number);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="frm-control-input" name="multi">
<input type="text" class="frm-control-input" name="price">
<button type="button" class="btn" value="1">1</button>
<button type="button" class="btn" value="2">2</button>
<button type="button" class="btn" value="3">3</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句