[英]increase variable random with onclick
如何計算所按單個鍵的總和?
這是代碼:
$ (function () {
var setup = generateOptions ();
var tot = 0;
$ ("# button1"). val (setup.opzione [0]);
$ ("# button2"). val (setup.opzione [1]);
$ ("# button3"). val (setup.opzione [2]);
$ ("# Button4.") val (setup.opzione [4]);
$ ("# Button5.") val (setup.opzione [5]);
$ ("# 6 button.") val (setup.opzione [6]);
$ ("# pulsante7.") val (setup.opzione [7]);
$ ("# pulsante8.") val (setup.opzione [8]);
$ ("# pulsante9.") val (setup.opzione [9]);
$ ("input.finish.") on ("click", function () {
tot = tot +, and here I do not know that I have to put ...
alert (tot);
});
});
幾乎每個選項都給我一個數字,我希望當我單擊按鈕時值存儲在變量tot中,出現的數字是使用隨機函數的隨機選項...這些按鈕是:
<input type="button" class="finish" id="pulsante1">
<input type="button" class="finish" id="pulsante2">
<input type="button" class="finish" id="pulsante3">
<div > <input type="button" class="finish" id="pulsante4">
<input type="button" class="finish" id="pulsante5">
<input type="button" class="finish" id="pulsante6">
<div > <input type="button" class="finish" id="pulsante7">
<input type="button" class="finish" id="pulsante8">
<input type="button" class="finish" id="pulsante9">
</div>
您可以使用$(this).val()
訪問輸入的值,然后使用parseInt()
將其轉換為整數。 您的點擊處理程序應如下所示:
$("input.finish").on("click", function() {
tot += parseInt($(this).val());
alert(tot);
});
看看這個小提琴 ,我使用了1到10之間的隨機數,但是它可以與您生成的任何其他數字一起使用。 希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.