开发者

Setting the value of a textarea in jQuery

开发者 https://www.devze.com 2023-01-13 02:24 出处:网络
I am trying to set the default value for a form field in SharePoint and am having a bit of trouble getting the code to work in IE.I have tested Firefox and Chrome successfully.Any ideas why IE would n

I am trying to set the default value for a form field in SharePoint and am having a bit of trouble getting the code to work in IE. I have tested Firefox and Chrome successfully. Any ideas why IE would not be setting the value?

<script type="text/javascript" src="http://www.qg.com/shared/cache/jquery/142/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    setTimeout("setDefaultText()", 500);    
    function setDefaultText()
    {   
        var text = "Param 1:\n\nParam 2:\n\nParam 3:";

        var r1text = $("#ctl00_m_g_94a22119开发者_Python百科_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();
        var r2text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();

        if((r1text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }

        if((r2text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }
    }
</script>


What is the purpose of the setTimeout()?

If you're trying to make sure the DOM is loaded, then you should do this:

$(function() {   
        var text = "Param 1:\n\nParam 2:\n\nParam 3:";

        var r1text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();
        var r2text = $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val();

        if((r1text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl09_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }

        if((r2text + "").length == 0)
        {
            $("#ctl00_m_g_94a22119_a2e7_408c_aa27_c680b509802d_ctl00_ctl04_ctl10_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").val(text);
        }
});

I'm guessing the DOM wasn't loaded in 500 milliseconds, and your .val() was giving you undefined, so with the + "" you were ending up with a string "undefined".

0

精彩评论

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