Setting a textbox value by JQuery

前端 未结 2 2177
小蘑菇
小蘑菇 2021-02-13 16:59

I am setting a TextBox controls value via an ajax post.

$(\'#txtSite\').val(msg.d.SiteName);

This is working and the value of the

相关标签:
2条回答
  • 2021-02-13 17:27

    You have your textbox set to Enabled="false" which renders in the browser with a disabled="disabled". Disabled form inputs are not submitted.

    The solution is either to make the textbox enabled and read-only:

    txtSite.Enabled = true;
    txtSite.Attributes.Add("readonly", "readonly"); //on prerender event or somewhere else
    

    or to use a different element set with runat="server", like a <asp:HiddenField /> and update both the textbox and the alternate element with your AJAX call:

    success: function(msg) {
        if (msg.d != null) {
            $('#txtSite').val(msg.d.SiteName);
            $('#hiddenInput').val(msg.d.SiteName);
        } else {
                $('#txtSite').val('');
        }
    }
    
    0 讨论(0)
  • 2021-02-13 17:33

    Yes, I definitely think, your text box should have a name attribute in html code, check for it. Without a 'name' attribute browser won't post this input box's data to server and thus you won't get it in server side c# code. Hope, this make sense..

    0 讨论(0)
提交回复
热议问题