0

私は基本的にJavaScriptを使用して、互いに依存する2つのテキストボックスを作成したいと考えています。たとえば、textbox1 に数値 x を入力すると、textbox2 が x*2 で即座に更新されます。x を textbox2 に入力すると、textbox1 が x/2 で自動的に更新されるようにします。これらの値は常に数値です。

編集:ここに私のhtmlファイルがあります。なぜそれが機能しないのか、誰かにヒントを教えてもらえますか?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $("#text1").keyup(function(){
            $("#text2").val($(this).val() * 2);
        });
        $("#text2").keyup(function(){
            $("#text1").val($(this).val() / 2);
        });        
    </script>
</head>
<body>
    <input type="text" name="text1" size=3 maxlength=6>
    <input type="text" name="text2" size=3 maxlength=6>
</body>
</html>
4

5 に答える 5

2

非常に単純なケースでは、これでうまくいくはずです(ここでフィドル):

// in a <script>:
window.update = function() {
  var one = document.getElementById('one'),
      two = document.getElementById('two');    

  two.value = parseInt(one.value) * 2;
}​

<!-- in your HTML: -->
<input id="one" type="text" onchange="update();" />
<input id="two" type="text" />​
于 2012-05-10T20:20:23.443 に答える
1
$("#textbox1").onkeyup(function() {
    $("#textbox2").val($(this).val() * 2);
});

$("#textbox2").onkeyup(function() {
    $("#textbox1").val($(this).val() / 2);
});
于 2012-05-10T20:19:45.573 に答える
0

I'm unable to test out the exact code you'll need for this right now, but the basic idea is to put an OnKeyUp event on each box and modify the other box through the script (using getElementById or whichever method you like)

I will try to put together some code when I can if someone else doesn't get to it first.

于 2012-05-10T20:18:01.770 に答える
0

onchange イベントまたは jquery change コールバックを使用して、他のボックスを更新します。

于 2012-05-10T20:19:47.977 に答える
0

この場合、これでうまくいくはずです: http://jsfiddle.net/du09jhpd/

window.update = function() {
  var one = document.getElementById('one'),
      two = document.getElementById('two');    

  two.value = parseInt(one.value) * 2;
}
window.update1 = function() {
  var one = document.getElementById('one'),
      two = document.getElementById('two');    

  one.value = parseInt(two.value) / 2;
}
于 2017-11-03T02:57:41.920 に答える