开发者

jQuery to bind input value to span or div?

开发者 https://www.devze.com 2023-02-09 19:30 出处:网络
Please excuse me if this is a newbie question. I am new to thi开发者_C百科s. I would like that a user fill out a form with their info and, just before clicking submit, there to be a summary of detail

Please excuse me if this is a newbie question. I am new to thi开发者_C百科s.

I would like that a user fill out a form with their info and, just before clicking submit, there to be a summary of details area where they confirm their details are correctly entered.

So is it possible for me to "bind" specific inputs to specific spans or divs and as they type (or maybe onBlur?) the span would reflect what is written in the respective input???


Populate div with id somediv with whatever is typed the input with id inputid each time a key is pressed:

$('#inputid').keyup(function() {
    $('#somediv').html($(this).val());
});


$('#myFormField').bind('change', function(){
 $('#myTargetSpan').text($(this).val());
});


Two most obvious possibilities

  1. Use jQuery Data Link plug-in that is able to bind input elements to other elements or even to JavaScript objects. Either a one-way or a two-way link can be established.

    or

  2. Use this code to update div

    on each keystroke

    $("#YourInputID").keyup(function(){
        $("#YourDivID").text(this.value);
    });
    

    when user leaves input

    $("#YourInputID").blur(function(){
        $("#YourDivID").text(this.value);
    });
    
0

精彩评论

暂无评论...
验证码 换一张
取 消