Calculation on vTiger field before save handler on Javascript Side (code inside)

眉间皱痕 提交于 2019-12-13 03:24:28

问题


I need to understand how to do real time calculation on Edit.js

By searching and looking around i came up with this code in Edit.js of the Contact Module.

calculate_amount: function (){
var units = $("input[name='cf_852']");
var value = $("input[name='cf_854']");
$(units, value).on('keyup', function(){
if (units.val() != '' && value.val() != ''){
var currentamount = units.val() * value.val();
$("input[name='cf_856']").val(currentamount);
}
});
}

Have i done something wrong? Because it doesn't work..

Thanks for all the help!


回答1:


I should write your keyup function like this:

I tested, it's OK

calculateAmount: function (){  
  var units = $("input[name='cf_1512']");
  var value = $("input[name='cf_1514']");
  $(document).on('keyup',"input[name='cf_1512'], input[name='cf_1514']", function(){
    if (units.val() != '' && value.val() != ''){
      var currentamount = units.val() * value.val();
      $("input[name='cf_1518']").val(currentamount);
    }
  })
},



回答2:


You must call you function into the function registerBasicEvents. If the registerBasicEvents function is not available in Edit.js of Contacts module so add it.

registerBasicEvents: function (container) {
  this._super(container);
  this.calculate_amount();
}



回答3:


You should pass id's of element instead reference of element for Keyup function. Please find below code snippet and modify your code in vTiger. As I checked all syntax and function written correctly in your script. Just pass the comma separated id's and execute the code. Thanks!

var units = $('#Contacts_editView_fieldName_cf_1512');
var value = $('#Contacts_editView_fieldName_cf_1514');
  
  $('#Contacts_editView_fieldName_cf_1512, #Contacts_editView_fieldName_cf_1514').on('keyup', function(){
 
        if (units.val() != '' && value.val() != ''){
           var currentamount = parseFloat(units.val()) * parseFloat(value.val());
           $("#Contacts_editView_fieldName_cf_1516").val(currentamount);
        }
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
Input1: <input type="text" id="Contacts_editView_fieldName_cf_1512"/><br/>
Input2: <input type="text" id="Contacts_editView_fieldName_cf_1514" /><br/>
Result: <input type="text" id="Contacts_editView_fieldName_cf_1516"/>


来源:https://stackoverflow.com/questions/56628996/calculation-on-vtiger-field-before-save-handler-on-javascript-side-code-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!