0

javascriptを使用して減算および追加のテキストボックス値に使用できるコードがあり、それは機能していますが、問題は、javascriptがonfocus textbox1回だけ必要なときにいつでも関数を何度も実行することexecuted functionです。

javascript 関数 何度も何度も追加onMouseOver="return B(0);"

JavaScript関数何度も減算onfocus="return C();"

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" >
4

3 に答える 3

1

あなたが持つことができます:

var executedAlready = false;

内部関数 B と C には次のものがあります。

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

それとも、代わりにイベントを切り離すことができますか? これを行うにはいくつかの異なる方法があると思います。

于 2013-03-27T12:45:19.067 に答える
0

他の答えは、結果を得る「最も速い」方法は、関数を1回だけ実行することだと言っています。次のようにできます。

  • フラグを作成します (関数が既にトリガーされているかどうかを知る変数のみ)。
  • 関数を実行するときは、まずこのフラグをチェックしてください。

関数 B() でそれを行う方法の例を次に示します。

注:私はあなたの機能を変更していません。今は入りたくありません)

// 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;
};

次に、C および D 関数で同じことを繰り返します (さらに 2 つのフラグを設定します)。

これは最善の方法ではありません。グローバル オブジェクトなどをセットアップするのは良くありませんが、おそらくサイド ライブラリを取得していないため、今のところ問題を解決するのに役立ちます。長期的な解決策としては、イベント ライブラリ ( YUI Eventなど) を使用して、onfocus イベントへのアクションのアタッチとデタッチを処理する必要があります。

于 2013-03-27T14:13:47.993 に答える
0

1 つ以上のフラグを使用できます。

ページの冒頭:

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

そしてあなたの要素で:

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

onFocusでも同じ...

于 2013-03-27T12:43:15.260 に答える