2015-04-14 77 views
2

尋找一個腳本,將按鈕單擊時將輸入值1複製到輸入2並添加+1以設置文本框。將輸入1中的值複製到輸入2 onclick

b = document.getElementById('tussenstand').value; 
 
var theTotal1 = b; 
 

 
$(document).on("click", "#button1", function(){ 
 
    theTotal1 = Number(theTotal2) 
 
    $('#eindstand').val(theTotal2);   
 
}); 
 
$('#eindstand').val(theTotal2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<input id="tussenstand"></input> 
 
<input id="eindstand"></input> 
 
<input id="sets"></input> 
 

 
<button id="button1">click</button>

在此先感謝。

+1

'theTotal2'在哪裏設置?你究竟想要達到什麼目的?即使給你的代碼,它也不是很清楚。 –

+0

爲什麼你要在html和javascript中添加click事件? – jcubic

+0

哦,對不起,我沒有必要現在看到它。 @jcubic –

回答

3

我認爲這樣做。

$(document).on("click", "#button1", function(){ 
 
    var total = document.getElementById('tussenstand').value; 
 
    $('#eindstand').val(total);  
 
    var sets = parseInt($('#sets').val()); 
 
    $('#sets').val((sets || 0) + 1); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<input id="tussenstand"></input> 
 
<input id="eindstand"></input> 
 
<input id="sets"></input> 
 

 
<button id="button1" onclick="">click</button>

+0

它很好,謝謝你! @KJ價格 –

+0

沒問題!很高興我能幫上忙! :D –

+0

'$(document).on(「click」,「#button1」,...'應該是'$(「#button1」)。on(「click」,...' –

1
$('#button1').click(function(){ 
    $('#eindstand').val($('#tussenstand').val()); 
    $('#sets').val(Number($('#sets').val())+1);  
}); 

檢查這裏:jsfiddle

編輯爲您的.on()方法評價

+0

嗨@shaunak shukla謝謝,但我需要從1開始輸入3,並比onclick添加1. –

+0

@JoranDob,現在檢查我的答案,它有更少的代碼和它的工作 –

1

看那JQuery API Documentation。該函數不會將目標作爲參數,而是作爲調用者對象!編輯:嗯,它實際上仍然會以另一種方式工作,但這使事件委派。只有當你知道你在做什麼時才這樣做。我喜歡這個改變:

$(document).on("click", "#button1", function(){ ... }); 

到這一點:

$("#button1").on("click", function() { ... }); 

其中在香草JS是:

document.getElementById("button1").addEventListener("click", function() { ... }); 

接下來,你不應該需要定義你的函數之外的變量,並用數字命名變量是一個不好的習慣。儘可能使名稱儘可能清晰。現在

,這是明確的,這裏就是我會寫:

$("#button1").on("click", function() { 
    $("#eindstand").val($("#tussenstand").val()); 
    $("#sets").val(parseInt($("#sets").val())+1);  
}); 
0

爲了實現這一用途:

$(function() { //on DOM ready 
 
    
 
    $('#button1').click(function(){ //Attach event 
 
    
 
    //Get value safe - can use parseFloat() too: 
 
    val1 = parseInt($('#tussenstand').val()); 
 
    val2 = parseInt($('#eindstand').val()); 
 
    sets = parseInt($('#sets').val()); 
 
    
 
    //Make sure we are using integers: 
 
    if (isNaN(val1) || isNaN(val2) || isNaN(sets)) return; 
 
    
 
    //Add 
 
    $('#eindstand').val(val1 + val2); 
 
    
 
    //Increment: 
 
    $('#sets').val(sets+1); 
 
    
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input id="tussenstand" type='number' /> 
 
<input id="eindstand" value='0' type='number' /> 
 
<input id="sets" value='0' type='number' /> 
 

 
<button id="button1">click</button>

1

下面的代碼應該工作。有幾個問題將在未來幫助你。在HTML中,通過onclick觸發的函數會干擾jQuery的onclick。你可能想要刪除它。

onclick="bereken(); 

你沒有聲明你有你的代碼的方式b變量。

b=document.getElementById('tussenstand').value; 

所述的jQuery的onclick被寫入應具有更窄的範圍(而不是文件)的方法。現在每次點擊任何文件時,都會在文件中觸發它。我改變了這一點:

$(document).on("click", "#button1", function(){ 

這樣:

$("#button1").on("click", function() { 

完整編輯的代碼是在這裏。

var count = 0; 
$("#button1").on("click", function(){  

    if (typeof b === 'number') { 
     count++; 

     $("#eindstand").val(b); 
     $("#sets").val(count); 
    }  
});