Home > front end >  Dynamically generated and the name of different controls, how to use ajax automatically saved data?
Dynamically generated and the name of different controls, how to use ajax automatically saved data?

Time:12-12

I according to the background sound of list elements in JSP, automatically generate a number of controls, for the convenience of the difference between receiving submitted the background, the name of each control (name) is different, for example abc1, abc3 etc., now I want to use ajax to automatically save these controls to fill in content, and found a big trouble: controls the name is too much, can not list, do you have master know how to deal with this kind of circumstance, if do not take the name changed to the same)?

CodePudding user response:

With jq $("... ") for the automatic generation control, to declare an empty object, cycle out each control's name and the value added to the object, send this object with ajax,

CodePudding user response:

Dom elements is also object, you can pick out these elements, put in an array, iterate over the line,

CodePudding user response:

@ the sky waves @ bosaidongmomo excuse me is there a specific code a little, or links to consult, thank you.

CodePudding user response:

To traverse the variable named this format, you can use the eval (), defect is unsafe, js injection favorite is that you write a lot of eval,
 
Var a1=0;
Var a2=6;
Var a3=12;
for(var i=0; i<3; I++) {
The console. The log (eval (" a "+ I));//the output: 0, 6, 12
}

Actually better solution should be avoid defining such meaningless variable names, can define an array to store the values, if is worth a lot of, can also use json to optimize space,

CodePudding user response:

Cc
reference 3 floor response:
@ the sky waves @ bosaidongmomo excuse me is there a specific code a little, or links to consult, thank you.

Can also use the jquery. Serialize () method to obtain all controls in the form of serialized string, assigned to ajax's data can be directly,
 
$. Ajax ({
Url: "XXXXXXX",
Type: "POST",
Data: $(" form "). The serialize (),
Success: the function (data) {
}
});


CodePudding user response:

Var from ABC, a variable cycle list each loop back together a I

CodePudding user response:

JQuery obtain all input is $(" input ")
JSP should also have access to the form of function according to the form type is not in accordance with the name for the access
  •  Tags:  
  • Ajax
  • Related