繁体   English   中英

如何使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM