简体   繁体   English

Jquery - 动态地将参数传递给函数

[英]Jquery - Dynamically pass parameters to function

I'm not even sure I'm asking or going about this the right way but it's probably easier if I just show it. 我甚至不确定我是以正确的方式询问或谈论这个问题,但如果我只是展示它,它可能会更容易。 Basically, I'm trying to have run an attr of each div as a parameter through a function and have it return a different result based on that div's attr. 基本上,我试图通过function运行每个divattr作为parameter ,并让它根据div的attr返回不同的结果。

The example, as you can see, is a group of dropdowns that appear when you click on a link in the container div. 您可以看到,该示例是单击容器div中的链接时显示的一组下拉列表。 If you make a selection it saves that as a attr in the parent div. 如果您进行选择,则将其保存为父div中的attr。 The problem arises when you click out, then back in on the container ... instead of reshowing each dropdown with the appropriate default or selection showing, it just mirrors the result of the a next to it. 当您单击,然后返回容器时出现问题...而不是使用相应的默认或选择显示重新显示每个下拉列表,它只是镜像旁边的a的结果。

http://jsfiddle.net/nosfan1019/b7F6x/5/ http://jsfiddle.net/nosfan1019/b7F6x/5/

TIA TIA

I inserted some console.log() statements to see what was happening with your various jQuery selectors. 我插入了一些console.log()语句来查看各种jQuery选择器发生了什么。 I observe the following: 我观察到以下内容:

  • when I click in the first "click" node, _container is "top one" 当我点击第一个“点击”节点时, _container是“顶级”节点
  • thus in your iteration of the three divs, you select both divs with class 'dd' contained in the div with class 'top one' 因此,在你的三个div的迭代中,你选择div中包含类'dd'的两个div,类为'top one'
  • the parameters _attr and _parent that you pass to your function select() are the same for each node that is processed, giving the same result for both 'dd' boxes. 传递给函数select()的参数_attr_parent对于每个处理的节点都是相同的,两个'dd'框的结果相同。

I think you want to change the selectors you use to locate the nodes to modify. 我想你想要改变你用来定位要修改的节点的选择器。

foo = foo.find('.dropdown-toggle').html(_new + '<b class="caret"></b>');

with this line you get two divs and hence you've change both values(in case the value was chosen from the droplist). 使用此行,您将获得两个div,因此您可以更改这两个值(如果从droplist中选择了值)。

To restore selected values correctly: 要正确恢复所选值:

function modified(_select) {
    console.log("modify");
    foo = $('#box').html();
    foo = $(_select).html(foo);
    // iterate on collection to restore selected value from selection tag;
    foo.filter("div[selection]").each(function(i, v){
        var selected = $(v).attr('selection');
        $(v).find('.dropdown-toggle').html(selected + '<b class="caret"></b>');                
    });                
}

Then, it's needed to be checked if any of parentDiv has [selection] attr: 然后,如果parentDiv中的任何一个有[selection] attr,则需要检查它:

if($(y).filter("div[selection]").length > 0){
    return modified(y);
}         

http://jsfiddle.net/b7F6x/50/ http://jsfiddle.net/b7F6x/50/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM