Inserting text into a textarea using .load() in jQuery
having a bit of a nightmare, I am trying to use jQuery to insert some text taken from a .load() call into a form field (textfield) after a user logs in (basically prefilling some known details). It is #bookName and #bookEmail I am having problems with
The code I am using is:
$.ajax({
type: "POST",
url: "/ajax/login.php",
data: dataString,
success: function(html) {
if (html == 1) {
$("#loginPanel").load("/ajax/login-panel.php");
$("#bookName").load("/ajax/getSessionDetails.php #userUsername");
$("#bookEmail").load("/ajax/getSessionDetails.php #userEmail");
$("#bookingLogin").hide("fast");
} else {
$("#loginError").html("Sorry, login failed, please try again");
}
}
});
If I hardco开发者_StackOverflow社区de such as $("#bookName").html("Test Content");
it works OK so it must be a problem with the .load call.
I looked around and found some guy suggest something like the following code but I couldn't get it to work:
$.get(htmlBannerUrl, function(data){
$('textarea').val(data);
});
You can also use text();
$('#loginError').text();
It is possible that you are firing too many simultaneous ajax requests:
$("#loginPanel").load("/ajax/login-panel.php");
$("#bookName").load("/ajax/getSessionDetails.php #userUsername");
$("#bookEmail").load("/ajax/getSessionDetails.php #userEmail");
The HTTP 1.1 spec advises clients to only allow 2 simultaneous connections to any originating host. You might try reducing those to a single one within $.ajax's success callback, to see if it works as expected. Also see:
How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
Well I had to use a workaround to do this, not nice at all but at least let me stick it to the background until the rest of the build is done.
Instead of adding it as a value I had to build a dupe of the whole form in a new php page and insert it using .load. Good thing is that it works, bad thing is that it isn't that useful if I want to reuse the code, I will have to build another page.
I should keep this question open as I have only found a workaround, is that how Stackoverflow works?
精彩评论