简体   繁体   中英

How to change the display value of two text boxes through the input value of another text box using javascript?

Hello i have just started to learn javascript and want to change the value i two text boxes through user input in another. The problem is that nothing happens to the two input fields attr-cost and attr-dice then the user inputs a number in attr-score. Here are the input fields in 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>

And here is the javascript/Jquery code:

$(".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";
    }
}

The problem is that the value of attr-score input field should change what is displayed in the fields attr-cost and attr-dice. So if the user inputs the number 1 in attr-score then attr-cost should show 1 and attr-dice should show 1d4. The problem is that the attr-cost and attr-dice don't respond to the input in attr-score. Attr-cost and attr-dice still remain empty. If there ia need for more html code i will provide it.

$(".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";
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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