Copy another textbox value in real time Jquery

后端 未结 5 612
臣服心动
臣服心动 2021-01-06 09:30

I want to get the value of another textbox and input it in realtime into the other textbox. HOW CAN I DETECT IF TEXT_3 WAS CHANGED? IF TEXT_3 VALUE CHANGED, IT MUST BE INPU

相关标签:
5条回答
  • 2021-01-06 09:33

    The problem is that you can't bind special event to check that the textbox value was changed using JavaScript and not manually. To solve the task, one option is to use the same keyup event for both text_1 and text_2. JQuery will add the new handler to the existing handlers:

    $("#text_1, #text_2").keyup(function(){
        $("#text_4").val($("#text_3").val());
    })
    

    DEMO: http://jsfiddle.net/8eXRx/11/

    0 讨论(0)
  • 2021-01-06 09:44

    Add change() after your textbox3.val(), like below:

    $("#text_1").keyup(function() {
        $("#text_3").val($("#text_1").val() + " " + $("#text_2").val()).change();
    })
    
    /* INPUT TEXT_1 AND TEXT_2 VALUE TO TEXT_3 ON TEXT_1 AND TEXT_2 KEYUP*/    
    $("#text_2").keyup(function(){
        $("#text_3").val($("#text_1").val() + " " + $("#text_2").val()).change();
    })
    

    http://jsfiddle.net/8eXRx/12/

    0 讨论(0)
  • 2021-01-06 09:46

    Try this.. n1 and n2 are ids of textbox

     $(document).ready(function(){
    
        $(':input').keyup(function(){
    
          $("#n2").val($("#n1").val()).change(); 
    
        });
    });
    
    0 讨论(0)
  • 2021-01-06 09:48

    Try:

    $("#text_3").keyup(function(){
       cur_val = $(this).val(); //grab #text_3's current value
       $(this).val(cur_val); // this is optional, but keep for sake
       $("#text_4").val(cur_val); //set #text_4's value as what #text_3 is having
    });
    
    0 讨论(0)
  • 2021-01-06 09:49

    the change event fires after the input field has lost it's focus. If you want to update it in realtime you also need the keyup event, so something like this:

    $("#text_3").keyup(function(){
        $("#text_4").val($("#text_3").val());
    })
    
    0 讨论(0)
提交回复
热议问题