Jquery,链接动态添加的选择

发布于 2024-12-22 06:15:36 字数 940 浏览 0 评论 0原文

我在使用 这个select 配合使用的 jQuery 插件时遇到问题s。

这里是我的 HTML 和 Javascript 代码。它有点大,主要是因为 select 中有很多 option

一开始它工作得很好——它成功地复制了目标元素。但在那之后,它就停止工作了。

我已经尝试修复它两天了,但不知道该怎么办。我警告插件中的所有变量,发现备份变量没有正确的数据,并且当它使用#location[2]创建元素时它还调用 #location[1] 的插件。

更好地描述问题 我对 fe 进行了三个选择:品牌、型号、颜色,

假设值可以是:

品牌: Iphone、Nokia; 适用型号: 4s,5800; 颜色:黑色、白色、蓝色、红色

,并且应该只有这些选项:

Iphone -> 4s->黑/白

诺基亚-> 5800——>蓝色/红色,

所以当我选择诺基亚时 -> 5800,我不想显示“白色”或“黑色”选项。

我为此使用了 jQuery 插件(我在上面链接了它),它工作得很好。

问题是: 我有一个按钮可以添加另一行该选择,并且该插件在插入多行后停止工作。我知道问题出在带有变量 backup 的插件中,

谁能告诉我是什么原因导致了这个问题吗?

I'm having a problem with this jQuery plugin that works with selects.

Here's my HTML and Javascript code. It's a bit large, primarily because of the many options in the select.

It works fine at first—it successfully copies the target elements. But after that, it stops working.

I've been trying to fix it for two days and don't know what to do. I was alerting all the variables in the plugin and found out that the backup variable doesn't have the right data and when it creates the element with #location[2] it also calls the plugin for #location[1].

Better description of problem
I had a three selects with f.e.: brand, model, color

lets say the values could be:

for brand: Iphone, Nokia;
for model: 4s, 5800;
for color: black, white, blue, red

and there' should be only these options:

Iphone -> 4s -> black / white

Nokia -> 5800 -> blue / red

so when I select nokia -> 5800, I didnt want an option "white" or "black" to be shown.

I used jQuery plugin (I linked it above) for this, and it works fine.

The problem is:
I have there a button to add another row of this selects and this plugin just stops working after inserting multiple rows. I know the problem is in plugin with variable backup

Can anyone tell what is causing the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-12-29 06:15:36

对于任何对此类有疑问的人,我以这种方式修复了它:

我使用 $().clone() 为每个选择创建了(备份)副本。然后在插入另一行选择后,我保存到 $(".select_to_be_chained").data("original_ref", $(".copy") ) 对该选择副本的引用,然后将其链接起来。

要实现此功能,您必须编辑 jquery.chained.js

找到:$(self).html(backup.html());

将其替换为:

// Select data from original_reference (if theres any)
// otherway select data from backup variable
if( ! $(self).data('original_ref') ) $(self).html(backup.html());
else $(self).html( $($(self).data('original_ref')).html() );

使用示例

// lets assume that we have in html 1st row of selects
// we only need to create a backup elements
var backup_brand = $("select.brand").clone(1);
var backup_model = $("select.model").clone(1);
var backup_color = $("select.color").clone(1);

// now we can chain that 1st row
$("select.color").chained( $("select.model") );
$("select.model").chained( $("select.brand") );

var row = 1;  // how many rows do we have?
function add_row()
{
    row++;
    // lets clone selects
    $("select.brand").clone(1);
    $("select.model").clone(1);
    $("select.color").clone(1);

    $(".all_three_selects").insertBefore("button"); // lets assume that we inserted all three selects in DOM

    // lets chain them
    // 1st we need to set reference to original data (backup)
    $("select.our_cloned_model").data("original_ref", backup_model );
    $("select.our_cloned_color").data("original_ref", backup_color );
    // now we can chain them
    $("select.our_cloned_color").chained( $("select.our_cloned_model") );
    $("select.our_cloned_model").chained( $("select.our_cloned_brand") );
}

我还没有测试过这个示例,但它知道了我如何使用它以及它的工作原理,我希望每个需要它的人都能理解它:)

For anybody that have a trouble with this class, I fixed it this way:

I made a (backup) copy of every select with $().clone(). Then after inserting another row of selects, I saved to $(".select_to_be_chained").data("original_ref", $(".copy") ) reference to that copy of select and than chained it.

To make this work, you have to edit your jquery.chained.js:

find this: $(self).html(backup.html());

replace it with:

// Select data from original_reference (if theres any)
// otherway select data from backup variable
if( ! $(self).data('original_ref') ) $(self).html(backup.html());
else $(self).html( $($(self).data('original_ref')).html() );

USAGE EXAMPLE

// lets assume that we have in html 1st row of selects
// we only need to create a backup elements
var backup_brand = $("select.brand").clone(1);
var backup_model = $("select.model").clone(1);
var backup_color = $("select.color").clone(1);

// now we can chain that 1st row
$("select.color").chained( $("select.model") );
$("select.model").chained( $("select.brand") );

var row = 1;  // how many rows do we have?
function add_row()
{
    row++;
    // lets clone selects
    $("select.brand").clone(1);
    $("select.model").clone(1);
    $("select.color").clone(1);

    $(".all_three_selects").insertBefore("button"); // lets assume that we inserted all three selects in DOM

    // lets chain them
    // 1st we need to set reference to original data (backup)
    $("select.our_cloned_model").data("original_ref", backup_model );
    $("select.our_cloned_color").data("original_ref", backup_color );
    // now we can chain them
    $("select.our_cloned_color").chained( $("select.our_cloned_model") );
    $("select.our_cloned_model").chained( $("select.our_cloned_brand") );
}

I haven't tested the example, but it got the idea how I use it and it works, I hope everybody that gonna need it will understand it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文