![](/img/trans.png)
[英]How to show total value of two text box in another box - Javascript
[英]How to change the display value of two text boxes through the input value of another text box using javascript?
您好我剛剛開始學習javascript並希望通過另一個用戶輸入更改兩個文本框的值。 問題是兩個輸入字段attr-cost和attr-dice沒有任何反應,然后用戶輸入attr-score中的數字。 以下是HTML中的輸入字段:
<td><input type="number" class="my-input attr-score" placeholder=" +" /</td>
<td><input type="number" class="my-input attr-cost" disabled></td>
<td><input type="text" class="my-input attr-dice" disabled></td>
這是javascript / Jquery代碼:
$(".attr-table.attr-score").on("keyup", scoreCostDice);
function scoreCostDice(){
var score = $(this).val();
var cost = calcCost(score);
var dice = calcDice(score);
$(this).parent().next().children(.attr-cost).val(cost);
$(this).parent().next().children(.attr-dice).val(dice);
}
function calcCost(score){
if (score == 0) {
return = 0;
}else (if score == 1) {
return = 1;
}else (if score == 2){
return = 3;
}else (if score == 3) {
return = 6;
}else (if score == 4) {
return = 10;
}else (if score == 5) {
return = 15;
}
}
function calcDice(score){
if (score == 0) {
return = "1d4";
}else (if score == 1) {
return = "1d4";
}else (if score == 2){
return = "1d6";
}else (if score == 3) {
return = "1d8";
}else (if score == 4) {
return = "1d10";
}else (if score == 5) {
return = "2d6";
}
}
問題是attr-score輸入字段的值應該改變字段attr-cost和attr-dice中顯示的內容。 因此,如果用戶輸入attr-score中的數字1,則attr-cost應顯示1,attr-dice應顯示1d4。 問題是attr-cost和attr-dice不響應attr-score的輸入。 Attr-cost和attr-dice仍然是空的。 如果我需要更多的HTML代碼,我將提供它。
$(".attr-score").on("keyup", scoreCostDice);
function scoreCostDice() {
var score = $(this).val();
var cost = calcCost(score);
var dice = calcDice(score);
$(this).parent().siblings().find('.attr-cost').val(cost);
$(this).parent().siblings().find('.attr-dice').val(dice);
}
function calcCost(score) {
if (score == 0) {
return 0;
} else if (score == 1) {
return 1;
} else if (score == 2) {
return 3;
} else if (score == 3) {
return 6;
} else if (score == 4) {
return 10;
} else if (score == 5) {
return 15;
}
}
function calcDice(score) {
if (score == 0) {
return "1d4";
} else if (score == 1) {
return "1d4";
} else if (score == 2) {
return "1d6";
} else if (score == 3) {
return "1d8";
} else if (score == 4) {
return "1d10";
} else if (score == 5) {
return "2d6";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.