如何使用ajax填充动态生成的选择框
我无法找到解决该项目中遇到的特定问题的方法。我需要以下内容:动态地将行添加到包含两个选择框的表中,并使用ajax根据第一个选择框的值自动填充第二个选择框的选择选项...在相应的表行中(这是由一些js动态添加的)。
还在我身边吗?
我可以根据第一行中第一个下拉列表的值,使用 ajax/javascript 轻松动态地填充选择下拉列表。我什至可以在同一行中填充三个或更多选择框,使用 ajax/js 函数加载每个附加选择。然而,这不是我的目标。
这就是我所拥有的...我的表单包含一个包含 3 列的简单表格。第一个是复选框,第二个是“州”选择框,第三列是“城市”选择框。表格上方有两个按钮,可让我“添加行”和“删除行”。请注意,第一行(已编程)非常适合使用 ajax 加载州的城市名称。
单击“添加行”按钮后,我可以动态创建第二行,其中包含复选框、州选择和城市选择字段。州选择是根据原始列的innerHTML 内容填充的,但城市选择显然为空,因为它期望ajax 来填充它。注意:这些字段的名称和 ID 名称是迭代的... city_id、city_id_1、city_id_2、city_id_3 等等...所以我可以使用这种唯一性。
我的问题在于,当我从第二行或第三行(依此类推)选择一个州时,只有第一个城市选择发生变化。为什么?因为js/ajax处理函数说只对名称为“city_id”的元素进行操作!这是第一行中城市选择框的名称。
我还没有找到一种方法来动态地将我需要更新的元素的名称传递给 onreadystate ajax 函数。当我需要在一行(同一行)中填充两个或三个选择框时,我必须为每个下拉列表提供互补的 ajax 函数 - 请注意,这些函数是静态的......但是如果我创建动态元素??我无法将那么多功能预先编程到我的页面中...或者我必须这样做吗?
如果您对如何实现这一目标有任何想法,我将非常感激!
谢谢!
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-populate 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)