简体   繁体   English

我有可用于使用JavaScript减去文本框值的代码吗?

[英]i have code that can be use for subtract the textbox values using javascript?

i have code that can be use for subtract and additional textbox values using javascript and it is working but problem is that javascript again and again executed function whenever onfocus textbox i want only one time javascript should be executed function ? 我有可用于使用javascript减去和附加文本框值的代码,它正在工作,但问题是每当onfocus textbox我只希望一次执行javascript函数时,javascript就会一次又一次executed function

javascript function again and again additional onMouseOver="return B(0);" javascript函数一次又一次地附加onMouseOver="return B(0);"

javascript function again and again subtraction onfocus="return C();" javascript函数一次又一次地减去onfocus="return C();"

javascript function again and again additional onfocus="return D();" javascript函数一次又一次地附加onfocus="return D();"

function getObj(objID){
return document.getElementById(objID);
}

function B(){
var advanceBox = document.getElementById('advance');
var originalValue = advanceBox.value;
advanceBox.onfocus = function() {
this.value = parseFloat(originalValue, 10) +
parseFloat(document.getElementById('recamt').value, 10);
return false;
};
}   

function C() {
getObj("balance").value=parseFloat(getObj("total").value  || 0)-
(parseFloat(getObj("advance").value || 0)) ;
getObj("balance").value=parseFloat(getObj("balance").value || 0)-
(parseFloat(getObj("discount").value)||0) ;
return false;
} 

function D() {
getObj("total").value=parseFloat(getObj("total").value  || 0)+
(parseFloat(getObj("openbal").value || 0)) ;
return false;
}      


 Opening Balance:<input class="input_field2" 
 type="text" name="openbal" id="openbal"><br />

Total:<input class="input_field2" type="text" 
readonly name="total" id="total" value="5000"><br />

Advance:<input class="input_field2" type="text" 
readonly name="advance" id="advance"    value="500" 
onMouseOver="return B(0);"><br />

Balance:<input class="input_field2" readonly type="text" 
name="balance" id="balance" onfocus="return C();"><br />

Rem Amount:<input class="input_field2" type="text"
name="recamt" id="recamt"><br />

Discount: <input class="input_field2" 
style="background-color:#FFF !important;" 
type="text" name="discount" id="discount" >

You could have: 你可以有:

var executedAlready = false;

An inside functions B and C have: 内部函数B和C具有:

if(executedAlready != true){ executedAlready = true; }
else { return; }

Or maybe you could detach the events instead? 或者,也许您可​​以分离事件 I guess there are a few different ways to do this. 我猜有几种不同的方法可以做到这一点。

you can use one or more flag(s) : 您可以使用一个或多个标志:

in the begenning of the page : 在页面的开头:

<script>
    var flag = false;
</script>

and on your element: 并在您的元素上:

<div .... onMouseOver="if(!flag) { flag = true; return B(0);}" > .... </div>

same for onFocus... 对于onFocus ...

What the other answers tell you that the "quickest" way to get results is to make your functions only execute once. 其他答案告诉您,获取结果的“最快”方法是使函数仅执行一次。 You can do that like this: 您可以这样做:

  • Make a flag (just a variable that knows if your function has been triggered already). 创建一个标志(只是一个知道您的函数是否已经被触发的变量)。
  • When executing your functions, first check on this flag. 在执行功能时,首先检查该标志。

Here's an example how to do it with function B(): 这是使用函数B()的示例:

( Note: I didn't change your function, don't wanna get into that now) 注意:我没有更改您的功能,现在不想进入该功能)

// setup fired as false
var hasBFired = false;
function B(){
  // if B is true, we do nothing
  if (hasBFired) {
    return;
  // else if it is not true, basically only the first time you call this, flip the flag and execute the rest of the code.
  } else {
    hasBFired = true;
  }
  var advanceBox = document.getElementById('advance');
  var originalValue = advanceBox.value;
  advanceBox.onfocus = function() {
  this.value = parseFloat(originalValue, 10) +
  parseFloat(document.getElementById('recamt').value, 10);
  return false;
};

Now, repeat the same with C and D functions (setup two more flags). 现在,对C和D函数重复相同的操作(设置另外两个标志)。

This is not the best way - it's not good to setup global objects and stuff, but since you probably aren't getting any side library in, it will help you solve your issue for now. 这不是最好的方法-设置全局对象和东西不是很好,但是由于您可能没有任何辅助库,因此它现在可以帮助您解决问题。 For long term solution, you should use an Event library (like YUI Event ) and have it handle attaching and detaching actions to onfocus events for you. 对于长期解决方案,您应该使用事件库(例如YUI Event ),并让它为您处理onfocus事件的附加和分离操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM