0

I have a form with several text fields. I want to change a span tag when I write something in the field. Like this:

Like this

Any ideas?

4

4 回答 4

5

You can use keyup and then replace the span content with the textbox value.

<input id="test" type="text" />
<span id="content"></span>

$("#test").keyup(function() {
    $("#content").html($("#test").val());
});

http://jsfiddle.net/

Edit:

You can also use $(this).val() as pXL has suggested.

$("#test").keyup(function() {
    var val = $(this).val();
    $("#content").html(val);
});
于 2013-05-22T14:42:05.387 回答
2

You can simply use jquery for this. For eg

<input type="text" id="mytext"/>
<span id="content"></span>

$(document).ready(function(){
    $("#mytext").keyup(function(){
            $('#content').html($(this).val());
        })
})
于 2013-05-22T14:47:22.300 回答
0

I would use $.keyup() and update the html with input value.
Demo: Working Demo

$(document).ready(function()
{
    // Regular typing updates.
    $("#input").keyup(function()
    {
        $("#output").html($(this).val());
    });

    // If pasted with mouse, when blur triggers, update.
    $("#input").change(function()
    {
        $(this).keyup();
    });

    // Any value loaded with the page.
    $("#input").keyup();
});
于 2013-05-22T14:48:27.217 回答
0

The lost focus is also an aproach,

html:

<input type="text" id="txa" value="123" />
<span>345</span>

jquery:

$('input[id=txa]').blur(function () {
    $(this).next("span").text($(this).val());
});

Glad to help.

于 2013-05-22T15:00:47.207 回答