开发者

jquery html() does not return changed values

I have a form with a textbox like this:

<html>
<head>
    <script type="text/javascript" src="jquery-1.6.2.js"></script>
</head>
<body>
    <input type="text" id="myTextBox" />
</body>
</html>

When I type something in myTextBox, the value is available with $("#myTextBox").val开发者_运维问答(), but is not represented if i do $("body").html(). How can I grab the html string and the updated form value too? Thanks!


You can use the .attr() function.

Example:

$("input").each(function(){
    $(this).attr("value", $(this).val());
});

After this you can do the:

    $("body").html();

This should work.


$('[type=text], textarea').each(function(){ this.defaultValue = this.value; });
$('[type=checkbox], [type=radio]').each(function(){ this.defaultChecked = this.checked; });
$('select option').each(function(){ this.defaultSelected = this.selected; });

then

$("body").html()


$('input').each(function(){
  $(this).attr('data-value-input', $(this).val());
});

and after that you can get your html

var bodyHtml = $('body').html().toString().replace('data-value-input', 'value');

and that is it.


Well there is one most simpler way of doing this, by employing the live function on the input.

$(document).ready(function() {

 var checker = false;
    $("input").live("change", function() {
        checker = (this.defaultValue !== this.value);
        if (checker)
            this.defaultValue = this.value;
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜