您好我刚刚开始学习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代码,我将提供它。
答案 0 :(得分:0)
$(".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";
}
}