![](/img/trans.png)
[英]how to check same values from two diffrent objects with diffrent number of value in Javascript
[英]how to take two buttons diffrent value by calling function in javascript
function getValue(){ var v = document.getElementById("btn2").value; document.getElementById("display").value += v; }
<input type ="text" id = "display" value = "" ></input> <tr> <td> <input type = "button" id = "btn2" value="7" onclick="getValue(this)" ></input> </td> <td> <input type = "button" id = "btn2" value="8" onclick="getValue(this)" ></input> </td> </tr>
我只想通過單擊8或7按鈕獲得不同的值。 但是每次我得到7的結果時,我可以做些什么,請幫忙..還有,我想在getValue(this)中傳遞“ this”,以便通過單擊該按鈕來獲取有關該按鈕的所有信息。 請幫我謝謝
您的ID(在dom中)應該是唯一的。 jQuery在調用$('#idstring')時僅返回第一個元素; 就像document.getElementById('#idstring')一樣。
在這種情況下,您可以使用className。
您也可以將參數傳遞給函數,但不要使用它。
我編輯了此代碼片段,以顯示如何在調用它之前將其綁定到該函數。 這樣,如果這是您的事,則可以使用“ this”。
function getValue(el) { document.getElementById("display").value = el.value; } function getValue2() { document.getElementById("display").value = this.value; }
<input type="text" id="display" value=""></input> <tr> <td> <input type="button" class="btn2" value="7" onclick="getValue(this)"></input> </td> <td> <input type="button" class="btn2" value="8" onclick="getValue2.bind(this)()"></input> </td> </tr>
確保您的ID是唯一的。
利用您要傳遞的參數( this
)。
function getValue(button){ var v = document.getElementById(button.id).value; document.getElementById("display").value += v; }
<input type ="text" id = "display" value = "" ></input> <tr> <td> <input type = "button" id = "btn1" value="7" onclick="getValue(this)" ></input> </td> <td> <input type = "button" id = "btn2" value="8" onclick="getValue(this)" ></input> </td> </tr>
我看到您已經有問題的答案。 但是,一種明確的方法可能是使用類。
$(".my_buttons").click(function(){
$("#display").val($(this).val());
});
<input type="text" id="display" />
<input type="button" class="my_buttons" value="7" />
<input type="button" class="my_buttons" value="8" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.