开发者

How to populate dynamically generated select boxes with ajax

开发者 https://www.devze.com 2022-12-17 13:54 出处:网络
I haven\'t been able to find a solution for the particular problem I\'m having with this project. I need to the following: dynamically add rows to a table that contain two select boxes AND use ajax to

I haven't been able to find a solution for the particular problem I'm having with this project. I need to the following: dynamically add rows to a table that contain two select boxes AND use ajax to auto-populat开发者_开发百科e the select options of the 2nd select box, based on the value of the first select box... in the respective table row (that was dynamically added on the fly by some js).

Still with me?

I can easily populate, dynamically, a select drop-down using ajax/javascript based on the value of the first drop-down, in the first row. I can even populate three or more select boxes in the same row, using ajax/js functions to load each additional select. Yet, this is not my goal.

Here's what I have... My form contains a simple table with 3 columns. First is a checkbox, the second is the "State" select box and the third column is the "City" select box. I have two buttons above the table that allow me to "Add Row" and "Delete Row". Mind you, the first row (which is programmed-in), works perfectly for loading the city names of a state using ajax.

Upon clicking the Add Row button, I'm able to create the second row dynamically, which contains the checkbox, state select and city select fields. The state select is populated based on the innerHTML content of the original column, but the city select is obviously null because it is expecting ajax to fill it. Note: the names and ID names of these fields are iterative... city_id, city_id_1, city_id_2, city_id_3 and so on... so I have that uniqueness to work with.

My problem lies in the fact that when I select a state from the 2nd or 3rd (and so on) rows, only the firstcity select changes. Why? Because the js/ajax processing function says to operate only on the element with name "city_id"!! Which is the name of the city select box in the first row.

I have not (yet) found a way to dynamically pass the name of the element I need updated to the onreadystate ajax function. When I've needed to populate two or three select boxes in a row (same row), I've had to have complementary ajax functions for each drop-down - which are static, mind you... but what if I create the element dynamically?? I can't pre-program that many functions into my page... or must I?

If you have any ideas on how to accomplish this I would be soo damn appreciative!!!

Thanks!


var i=1; // put here the number of the column being added now.
var x=document.getElementById("city_id_"+i);
// populate x
0

精彩评论

暂无评论...
验证码 换一张
取 消