JS/jQuery:从禁用/只读输入触发onchange,而无需使用按钮 [英] JS/jQuery: fire onchange from a disabled/readonly input without using button

查看:85
本文介绍了JS/jQuery:从禁用/只读输入触发onchange,而无需使用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是获取全年的总数量.

The goal is to get the total Qty for the whole year.

用户将数字输入3个不同的文本框(1月,2月,3月),然后将总和显示在禁用的文本框(第1季度)中. 现在我有4个实例,知道我们每年有4个季度.

The user will input numbers into 3 different textboxes(Jan,Feb,Mar), then the sum will be displayed into a disabled textbox(Quarter1). Now I have 4 instances of these knowing we have 4 quarters/year.

通过使用文本框附带的onchange()功能,我可以轻松地显示每季度的总和.

I can easily display the sum per quarter, by using the onchange() function attached to the textboxes.

现在我在从4个禁用的文本框中获取总和时遇到了问题,因为我们不能在其上使用onchange(),因为它已被禁用.

Now I am having issues getting the sum from the 4 disabled textboxes, knowing we can't use the onchange() on it because it's disabled.

我已经搜索并且可能只有在使用按钮时才能得到结果.

I have searched and probably getting results only when a button is used.

TLDR :我试图自动将四个禁用文本框的总和显示到另一个文本框,而无需用户单击任何按钮(就像触发onchange事件一样)

TLDR: I am trying to display the sum from the four disabled textboxes to another textbox automatically, without the user clicking any button(just like firing the onchange event)

我已经尝试过了,其中我试图显示第一季度的总价值,但没有用:

I have tried this one, wherein I tried to display the value of the first quarter to the total, and not working:

$(document).ready(function() {
    $('input[id$=yearlyTotal]').bind("displaytotal", function() {});
    $('#qtr1').change(function() {
        var mos = document.getElementsByClassName("quantityA");
        var mosCount = mos.length;
        var total = 0;

        for (var i = 0; i < mosCount; i++) {
            total = total + parseInt(mos[i].value);
        }
        $('input[id$=yearlyTotal]').val(total).trigger('displaytotal');
    });
});

希望有可能,在此先谢谢

Hope it's possible, thanks in advance

添加了用户界面

显示Q1(这与4个qtr相同)​​

Showing Q1 (its just the same for the 4 qtrs)

   <div class="form-group col-md-6">
           <label class="col-sm-1 control-label">Jan:</label>
           <div class="col-sm-2 small">
           <input type="number" min="0" id="col3" class="form-control input-sm monthly" data-q="q1" name="January"  />
           </div>
           <label class="col-sm-1 control-label">Feb:</label>
           <div class="col-sm-2 small">
           <input type="number" min="0" id="col4" class="form-control input-sm monthly" data-q="q1" name="February" />
           </div>
           <label class="col-sm-1 control-label">Mar:</label>
           <div class="col-sm-2 small">
           <input type="number" min="0" id="col5" class="form-control input-sm monthly" data-q="q1" name="March" />
           </div>
           <label class="col-sm-1 control-label">Q1:</label>
           <div class="col-sm-2 small">
           <input type="text" min="0" id="q1" class="form-control input-sm quarter" name="q1"  style="background-color: #b3dcf5;"  disabled />
           </div>
</div>

这是总数量的div

<div class="col-md-6">
       <label class="col-sm-3 control-label" id="">Total Quantity:</label>
       <div class="col-sm-3 small">
       <input type="text" id="final" class="form-control input-sm" name="TotalQuantity" value="0" disabled />
       </div>
</div>

推荐答案

方法1:

基本上,您需要做的是使用jQuery 我不知道您的HTML的结构-这就是为什么总是建议提供 MCVE 示例的原因-我制作了一个演示示例,但做事方式有所不同,如下所示:

As I don't know how your HTML is structured -this why it is always recommended to provide MCVE example- I made a demo example and I've done things differently, like below:

jsFiddle 1

var monthly = $('.monthly'),
  Qrt = $('.quarter');

monthly.on('change, input', function() {
  var $th = $(this),
    // depending on the value of the data-q attribute, we pick
    // all input fields with the same data-q as an array, then
    //loop through them adding their values up
    q = $th.attr('data-q'),
    qArray = $th.parent().children('input[data-q="' + q + '"]'),
    tempSum = 0;

  for (var i = 0, ln = qArray.length; i < ln; i++) {
    tempSum += +$(qArray[i]).val();
  }
  
  // we pick the corresponding quarter sum field, again depending
  // on the value of the data-q attritues, and update its value, then
  // we trigger the change event of this quarter sum field.
  $('#' + q).val(tempSum).trigger('change'); // here you trigger it
});

Qrt.on('change', function() {
  var qSum = 0;
  for (var i = 0, ln = Qrt.length; i < ln; i++) {
    qSum += +$(Qrt[i]).val();
  }
  $('#final').val(qSum);
});

.monthly { width: 32%; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<h3>Grand Total:</h3><input type="text" id="final" disabled><hr>
<h3>1st Q:</h3>
<input type="text" class="monthly" data-q="q1">
<input type="text" class="monthly" data-q="q1">
<input type="text" class="monthly" data-q="q1">
<br>Sum:<input id="q1" type="text" class="quarter" disabled>
<h3>2nd Q:</h3>
<input type="text" class="monthly" data-q="q2">
<input type="text" class="monthly" data-q="q2">
<input type="text" class="monthly" data-q="q2">
<br>Sum:<input id="q2" type="text" class="quarter" disabled>
<h3>3rd Q:</h3>
<input type="text" class="monthly" data-q="q3">
<input type="text" class="monthly" data-q="q3">
<input type="text" class="monthly" data-q="q3">
<br>Sum:<input id="q3" type="text" class="quarter" disabled>
<h3>4th Q:</h3>
<input type="text" class="monthly" data-q="q4">
<input type="text" class="monthly q-4th" data-q="q4">
<input type="text" class="monthly q-4th" data-q="q4">
<br>Sum:<input id="q4" type="text" class="quarter" disabled>

由于您对任何 .monthly 字段进行的更改都会更改相应的季度总和值,因此也会影响年度总和的值,因此您无需捕获禁用的四分之一和字段的 change 事件,仅遍历它们的值并更新Annual字段的值,所有这些都应在 on('change') 内完成 .monthly 字段的事件,如下所示:

since any change you make to any .monthly field will change the corresponding value of quarter sum, and thus it'll also affect the value of the yearly sum, you don't need to capture the change event of the disabled quarter sum fields, just loop through their values and update the value of the yearly field, all should be done inside the on('change') event of the .monthly fields, like below:

jsFiddle 2

jQuery

var monthly = $('.monthly'),
    Qrt = $('.quarter');

monthly.on('change, input', function() {
  var $th = $(this),
    q = $th.attr('data-q'),
    qArray = $th.parent().children('input[data-q="' +q+ '"]'),
    tempSum = 0,
    qSum = 0;

  for (var i = 0, ln = qArray.length; i < ln; i++) {
    tempSum += +$(qArray[i]).val();
  }
  $('#' + q).val(tempSum);

  // code below
  for (var i = 0, ln = Qrt.length; i < ln; i++) {
    qSum += +$(Qrt[i]).val();
  }
  $('#final').val(qSum);
});


更新:

对于OP中更新的HTML,请用以下内容替换qArray行:


Update:

For the updated HTML in the OP, replace qArray line with this one:

$th.parents('.form-group').find('input[data-q="' + q + '"]')`,

注意 parents() 带有"s"字母,与前一个parent()在DOM上移一个单层不同,它在DOM树中搜索这些元素的祖先,并从匹配的元素构造一个新的jQuery对象,因此它向上移动直到到达匹配的父级,这里是.form-group.

Note parents() is with "s" letter, unlike the former parent() which moves up a single level up the DOM, it does " search through the ancestors of these elements in the DOM tree and construct a new jQuery object from the matching elements ordered from immediate parent on up. ", so it travels up until we reach the matchng parent, here it is .form-group.

然后而不是children(),我们使用 find() .

Then instead of children(), we use find().

jsFiddle 3

这篇关于JS/jQuery:从禁用/只读输入触发onchange,而无需使用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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