简体   繁体   English

在document.ready中设置隐藏字段的值,并在pageload事件中获取其值

[英]set hidden field value in document.ready and get its value in pageload event

I am trying to set hidden fied value in document.ready function and get it in PageLoad event but its value always empty where I am wrong please suggest me. 我正在尝试在document.ready函数中设置隐藏的fied值,并在PageLoad事件中获取它,但是在我错的地方其值始终为空,请建议我。 My javascript code is 我的JavaScript代码是

    $(document).ready(function () {
        if (document.getElementById('<%=hdnMiddlediv.ClientID %>').value = '')
        { document.getElementById('<%=hdnMiddlediv.ClientID %>').value = 'test'; }
    });
</script>

c# C#

    protected void Page_Load(object sender, EventArgs e)
    {
        string val = hdnMiddlediv.Value;
        if (!IsPostBack)
        {
            string val2 = hdnMiddlediv.Value;

        }
    }

Use Comparision(==/===) operator instead of Assignment(=) operator. 使用Comparision(== / ===)运算符,而不是Assignment(=)运算符。 If you use assignment operator, the condition will always evaluate to false and your code inside if will never execute. 如果您使用赋值运算符,则条件将始终为false,并且if永远不会执行,则内部的代码将一直执行。

if (document.getElementById('<%=hdnMiddlediv.ClientID %>').value == '')
//                                                               ^^

If you're using jQuery use val() to get/set value of an element: 如果您使用的是jQuery使用val()来获取/设置元素的值:

$(document).ready(function() {
    if ($('#<%=hdnMiddlediv.ClientID %>').val()) {
        $('#<%=hdnMiddlediv.ClientID %>').val('test');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取在document.ready中设置的隐藏字段的值,该值位于asp.net代码后面 - Grab hidden field's value that is set in document.ready in asp.net code behind 如何在CasperJS中的document.ready(function(){})之后获取文本区域的值 - How to get value of a textarea after document.ready(function(){ }) in CasperJS jQuery document.ready vs pageLoad - jQuery document.ready vs pageLoad <div>$document.ready() 中没有 tab 值 - <div> tab value is not available in $document.ready() 如何获取设置在$(document).ready函数上的字段的值? - how to get value of a field which set on $(document).ready function? 准备好文档上的类,具体取决于字段值 - Set Class on document ready, depending on field Value 如何在没有任何事件的情况下从jquery的document.ready服务器端检索值 - How to retrieve the value from server side on document.ready in jquery without any event 文档就绪中填充的隐藏字段 - 值在外部函数中不可用 - Hidden Field populated in Document Ready - Value not available in outer functions javascript(jquery) - 代码应该在 - &gt; onload或document.ready或pageLoad()? - javascript (jquery) - codes should be on -> onload or document.ready or pageLoad()? 在pageload方法之后调用Document.ready方法-jQuery 3.3.2 - Document.ready method is called after pageload method - Jquery 3.3.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM