开发者

Modify tag value using JavaScript

开发者 https://www.devze.com 2023-02-12 03:48 出处:网络
I have this tag on a webpage <input type=\"hidden\" name=\"ctl00_ed3327f9_e1db_40db_9d66_15946cead7ae_STATEFIELD\" id=\"ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD\" value=\"0\" /&g

I have this tag on a webpage

<input type="hidden" name="ctl00_ed3327f9_e1db_40db_9d66_15946cead7ae_STATEFIELD" id="ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD" value="0" />

I want to modify its value, i.e. from value="0" to value="1" using JavaScript. I have tried:

document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD"); 

but it returns null. I guess that's because it i开发者_如何学Pythons a hidden field


If getElementById is returning null, it is likely because you're running the script before the element has been created.

Either move your script to the bottom of the page, just inside the closing </body> tag:

<body>

    <!-- page content -->

    <script type="text/javascript">
        document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").value = 1; 
    </script>
</body>

or run in inside a window.onload handler:

<script type="text/javascript">
    window.onload = function() {
        document.getElementById("ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").value = 1; 
    };
</script>


For .NET 3.5 and earlier, use

document.getElementById('<%= controlName.ClientID %>');

For .NET 4.0, set the ClientIDMode on the control to Static.


I just tested your code and it works. hidden shouldn't make a difference. I am guessing the problem might be that you are trying to run getElementById before the element has been loaded onto the page?


You've added the jquery tag, so I presume your are using it. This would be your solution:

$("#ctl00_0g_ed3327f9_e1db_40db_9d66_15946cead7ae__STATEFIELD").val("1");
0

精彩评论

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