开发者

jquery and input text

开发者 https://www.devze.com 2022-12-16 16:14 出处:网络
I would know if this is a bug... When I alert html content from #test, I get : <input name=\"sum\" value=\"\" type=\"text\">

I would know if this is a bug... When I alert html content from #test, I get :

<input name="sum" value="" type="text">

whereas it was set to 55 just before the alert, and I can view it on the brower.

Can you tell me why ? :-)

    <div id="test">
    <input type="text" name="开发者_运维技巧sum" value="">
</div>

<script language="javascript">
 $(document).ready(function() {
    $("#test").find("input[name='sum']").val(55);
    alert($("#test").html());
 });
 </script>


Javascript doesn't change the physical markup on the page. It changes the DOM. The DOM is constructed from the markup, but after that the markup isn't crucial.

You don't want to alert the HTML, you want to alert the value of the input:

alert($("#test :input[name='sum']").val());


This is not a bug. The value of #test is not part of the HTML content of the input -- it was set after the element was inserted into the DOM -- and thus the html() function doesn't return it.

0

精彩评论

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