Referencing variables on another page using Javascript.
I'm currently working on a sort of Administration method to be able to save certain text and to display this on another page.
Currently, the defaults in the "Admin" page are to be referenced from the other page. It's a simple page:
<asp:TextBox Text="Testing jQuery method" ID="TextBox" runat="server"/>
<asp:CheckBox Checked = "true" ID="CheckBox" runat="server" />
Now, the page that references these fields is actually a simple HTML file, so I'm trying to use jQuery to reference the fields. I'm trying to use a jQuery ajax call, but I can't seem to get it to work correctly.
<script type="text/javascript" src="Scripts/jquery-1.4.1.js">
var asp_checked;
var asp_text;
try {
jQuery.ajax({
url: '~/About.aspx',
success: function (response, status, xhr) {
if (status != "error") {
asp_checked = $('#CheckBox', response).text();
asp_text = $('#TextBox', response).text();
}
else { asp_text = "Error in jQuery Call"; }
},
async: false
});
document.write(asp_text);
}
catch (err) {
document.write("The jQuery is not working");
}
</script>
Things to note:
1. I don't really know much about jQuery in general. 2. I can't use cookies (I don't think). This is going onto our corporat开发者_如何学运维e intranet for announcements and such. 3. The jQuery code was taken from SO, but I can't recall the Question reference. When I do I will update here.Because it's an ASP control, keep in mind that ASP likes to make its own ("relative") ids for controls. Something like <asp:CheckBox id="cb" />
can turn in to <input type="checkbox" id="SomeContainer_foo$cb" />
once it hits the end result page. (This means using $('#cb')
is no longer valid because of all the other information now within the control's ID)
Your options?
- Change your control to use static ids by setting the ClientIdMode on the control. This makes whatever you use as an ID in the ASP markup transfer to the HTML page.
- Change your selector in jQuery to something like this:
$('[id$=cb]')
($= means "ends with")
精彩评论