I have a number of webpages on which I have the following piece of Javascript
var inputs = $("body :input");
for(i=0;i<inputs.length;i++)
{
thisObj =inputs[i];// test if the class 'roll' exists
if (thisObj.getAttribute('type') == 'text')
{
params+=thisObj.id + "="+thisObj.value+"&";
}
}
If the page it is called from does not have any querystring elements tagged on to the URL (i.e. localhost/default.aspx ) it works fine and par开发者_开发知识库ams is populated.
If however it is called from a page containing a querystring element in the url (i.e. localhost/default.aspx?ref=42) inputs is returned as an empty array and params is blank.
I know that I have probably missed something very obvious but for the life of my I cannot work out what the cause is.
Edit:
To be clearer if I simplify the code to:-
var par='';
var inputs = $("body :input");
par ="c="+inputs.length;
I get c=9 if the url does not have a GET parameter and an empty string if the url has a GET parameter
The code appears to be preparing a querystring (the variable params
) by collecting data from various input fields on a page, most likely for a subsequent redirect to a dynamically generated URL containing the form data in the form of GET parameters. Note that the jQuery selector "body :input"
effectively selects all elements within the entire page that can reasonably be used for user input (input, textarea, select and button, to be precise).
Thus, the code is likely intended to be used on the page where where the query string that you refer to is assebled, and not the one on which the parameters are read and put into use.
精彩评论