2009-10-24 91 views
4

对不起,如果这是已经发布我一直在寻找无果..通过嵌套的表单元素jQuery的迭代

我只是想知道如何通过嵌套形式“元素”(元素是循环不仅严格的形式元素,如输入标签,但其他HTML元素以及)在jQuery中。 目前我有这段代码做到这一点:

$('#'+arguments[i].formid).children().each(function(){ 
    var child = $(this); 
    alert(child.attr('id')); 
    if(child.is(":input")) { alert(child.attr('id')); 
    if(child.attr('id')!='') eval("p."+child.attr('id')+"='"+child.attr('value')+"'"); 
    } 

     if(child.is(":textarea")) { 
    if(child.attr('id')!='') eval("p."+child.attr('id')+"='"+child.attr('value')+"'"); 
    } 
    }); 

它不工作时,我的形式包含其他元素是这样的:

<form> 
    <div id='tabs'> 
     <ul>...</ul> 
     <div id='tab-1'> 
       <input type='text' id='fname' /> 
       <textarea id='desc' ></textarea> 
     </div> 
    </div> 
</form> 

请帮助...

+2

首先:从不在JS中使用eval。第二:你究竟想要达到什么目标?通常有更好的方法来迭代所有元素。 – RaYell 2009-10-24 11:18:26

+0

我使用eval()会导致我的对象属性是动态的,并且基于表单输入的id。还是有没有其他的方法来做到这一点,而不使用eval()? – jan 2009-10-24 12:18:56

+0

感谢您的提示。我只是不知何故需要一种方法来传递一个对象从PHP到JavaScript像从PHP我把这个值扔到JavaScript:{lastpage:'main',var:foo,var2:bar}并在javascript:eval(“p =”+ <?= $ thatobject>);这样我可以记录最新的会话值在当前的请求页面上使用..这有道理吗?我想我不是很擅长这个。 – jan 2009-10-24 12:37:43

回答

3

你可以做,在一个递归函数。

function doStuff(child) { 
    if(child.is(":input")) { 
    ... 
    } 

    if(child.is(":textarea")) { 
    ... 
    } 
} 

function walk(children) { 
    if (typeof children == "undefined" || children.size() === 0) { 
    return; 
    } 
    children.each(function(){ 
    var child = $(this); 
    if (child.children().size() > 0) { 
     walk(child.children()); 
    } 
    doStuff(child); 
    } 
} 

walk($('#'+arguments[i].formid).children()); 

编辑:我只是想通了,你正在尝试做的,你可以打破它这种

var p = {}; 
$('#'+arguments[i].formid + " input[id], #"+arguments[i].formid + " textarea[id]").each(function(){ 
    var child = $(this); 
    p[child.attr("id")] = child.attr("value"); 
}); 

你或许应该阅读更多关于jQuery的selectors

+0

哇。谢啦!解决了它,甚至删除了几行代码。你是对的我真的需要阅读更多关于jquery选择器,但我无法找到一个完整的例子,你可以遇到这么多的条件,将具体到我自己的要求.. jQuery网站给出的例子,但只是基本的使用$(“# formid> input“)。each(func ...我想我正在寻找一个解释,你可以在$()的大括号里做什么,我从来没有找到.. – jan 2009-10-24 12:33:26

+0

如果这解决了你的问题,你可以标记这个问题可以通过点击我答案旁边的绿色复选标记来解决。 – moxn 2009-10-24 18:29:33

8

你可以使用contents()(并根据需要筛选出文本节点)或find('*')以获取所有元素,但我不喜欢使用通配符。

$('form').contents() 
      .filter(function() { return this.nodeType == 1; }) 
      .each(...); 

$('form').find('*') 
      .each(...); 
0

说不上来,如果你需要jQuery的,请参见下面domIterator例如...

/* 
    menu.js  : main menu javascript class for ekerner.com 
    Author  : 'Eugene Kerner' <[email protected]> 
    Date  : 14-12-2007 
    Dependencies: menu.css 
       : an unordered list structure as per the below Example. 
       : a javascript call to menu.init(menuId, selectedMenuItemsArray) as per the below Example. 
    Notes  : if your menu link has an onclick that returns false then it needs to call menu.init before returning (see Example). 
    Example  : - 
    <ul id="mainMenu" class="menuItems"> 
     <li> 
     <a href="/">Menu Item</a> 
     <ul> 
      <li><a href="/webpage" onclick="window.open('/webpage'); menu.init('mainMenu', ['Menu Item', 'Sub Menu Item']); return false;">Sub Menu Item</a></li> 
     </ul> 
     </li> 
    </ul> 
    <script type="text/javascript">menu.init('mainMenu', ['Menu Item']);</script> 
*/ 

var menu = { 

    selectedItems : [], 

    init : function(menuId, selectedMenuItems) 
    { 
    this.selectedItems = selectedMenuItems; 
    var menuItem = domIterator.getFirstChild(document.getElementById(menuId)); 
    while (menuItem) { 
     var menuItemLink = domIterator.getFirstChild(menuItem); 
     var subMenu  = domIterator.getNextSibling(menuItemLink); 
     this.applySelectedClass(menuItemLink); 
     if (subMenu) { 
     menuItem.onmouseover = function(){menu.showSubMenu(this)}; 
     menuItem.onmouseout = function(){menu.hideSubMenu(this)}; 
     var subMenuLinks = subMenu.getElementsByTagName('a'); 
     for (var i = 0; i < subMenuLinks.length; i++) 
      this.applySelectedClass(subMenuLinks[i]); 
     } 
     menuItem = domIterator.getNextSibling(menuItem); 
    } 
    }, 

    applySelectedClass : function(menuLink) 
    { 
    for (var i = 0; i < this.selectedItems.length; i++) { 
     if (menuLink.innerHTML == this.selectedItems[i]) { 
     menuLink.className = 'selected'; 
     return; 
     } 
    } 
    menuLink.className = ''; 
    }, 

    showSubMenu : function(menuItem) 
    { 
    domIterator.getFirstChild(menuItem).className  = 'selected'; 
    domIterator.getLastChild (menuItem).style.display = 'block'; 
    }, 

    hideSubMenu : function(menuItem) 
    { 
    domIterator.getLastChild (menuItem).style.display = 'none'; 
    this.applySelectedClass(domIterator.getFirstChild(menuItem)); 
    } 

}; // end menu 

var domIterator = { 

    getFirstChild : function(elem) 
    { 
    if (elem) return domIterator.continueUntilType1(elem.firstChild, 'next'); 
    }, 

    getLastChild : function(elem) 
    { 
    if (elem) return domIterator.continueUntilType1(elem.lastChild, 'previous'); 
    }, 

    getNextSibling : function(elem) 
    { 
    if (elem) return domIterator.continueUntilType1(elem.nextSibling, 'next'); 
    }, 

    continueUntilType1 : function(elem, direction) 
    { 
    while (elem && elem.nodeType != 1) 
     elem = elem[direction + 'Sibling']; 
    return elem; 
    } 

}; // end domIterator 
1

回来后纠正自己:) jQuery的岩石! 这里速成班后,所以在jQuery的同样的事情...

/* 
    menu.jq  : main menu jQuery for ekerner.com 
    Author  : 'Eugene Kerner' <[email protected]> 
    Date  : 16-12-2009 
    Dependencies: jQuery javascript lib 
       : menu.css 
       : an unordered list structure as per the below Example. 
       : a javascript call to menu.init(menuId, selectedMenuItemsArray) as per the below Example. 
    Notes  : if your menu link has an onclick that returns false then it needs to call menu.init before returning (see Example). 
    Example  : - 
    <ul id="mainMenu" class="menuItems"> 
     <li> 
     <a href="/">Menu Item</a> 
     <ul> 
      <li><a href="/webpage" onclick="window.open('/webpage'); menu.init('mainMenu', ['Menu Item', 'Sub Menu Item']); return false;">Sub Menu Item</a></li> 
     </ul> 
     </li> 
    </ul> 
    <script type="text/javascript">$(document).ready(function(){menu.init('mainMenu', ['Menu Item'])});</script> 
*/ 

var menu = { 
    selectedClass : 'selected', 
    animateSpeed : 'fast', 
    selectedLinks : [], 
    init : function(menuId, selectedLinks) 
    { 
    $('#' + menuId).children('li').each(function(){ 
     var menuItem  = $(this); 
     var menuLink  = menuItem.children('a:first-child'); 
     var subMenu  = menuItem.children('ul:last-child'); 
     menu.selectedLinks = selectedLinks; 
     menu.applySelectedClass(menuLink); 
     if (subMenu.length == 1) { 
     menuItem.hover(
      function(){menuLink.addClass(menu.selectedClass); subMenu.slideDown(menu.animateSpeed)}, 
      function(){subMenu.slideUp(menu.animateSpeed); menu.applySelectedClass(menuLink)} 
     ); 
     subMenu.find('a').each(function(){menu.applySelectedClass($(this))}); 
     } 
    }); 
    }, 
    applySelectedClass : function(menuLink) 
    { 
    ($.inArray(menuLink.html(), menu.selectedLinks) > -1) ? menuLink.addClass(menu.selectedClass) : menuLink.removeClass(menu.selectedClass); 
    } 
} 

这里是万一有人想利用它的CSS ...

/* 
    menu.css - main menu cascading style sheet for ekerner.com all media 
    'Eugene Kerner' <[email protected]> 
    14-12-2007 
*/ 

.menuItems, .menuItems li, .menuItems li ul, .menuItems li ul li { 
    padding: 0; 
    margin: 0; 
} 
.menuItems, .menuItems li ul { 
    list-style: none; 
} 
.menuItems { 
    background: url(/shared/images/menu/menu_button_bg.png) repeat-x; 
    height: 30px; 
} 
.menuItems:after { 
    content: "."; 
    height: 0; 
    clear: both; 
    display: none; 
} 
.menuItems li { 
    width: 80px; 
    float: left; 
} 
.menuItems li a { 
    color: #0d2a86; 
    font-size: 14px; 
    font-weight: bold; 
    text-decoration: none; 
    text-align: center; 
    height: 24px; 
    padding-top: 6px; 
    border-right: 1px solid #f3f3f3; 
    display: block; 
} 
.menuItems li a:hover, .menuItems li .selected { 
    background: url(/shared/images/menu/menu_button_bg_selected.png) repeat-x; 
    color: #518ed3; 
} 
.menuItems li ul { 
    position: absolute; 
    z-index: 100; 
    border: 1px solid #e0e7ef; 
    border-top: 0; 
    display: none; 
} 
.menuItems li ul li { 
    width: 77px; 
    clear: both; 
} 
.menuItems li ul li a { 
    background: #f3f3f3; 
    font-size: 12px; 
    font-weight: normal; 
    height: 18px; 
    padding: 0; 
    padding-top: 2px; 
    border: 0; 
} 
.menuItems li ul li a:hover, .menuItems li ul li .selected { 
    background: #e0e7ef; 
} 
.visible { 
    display: block; 
} 
.hidden { 
    display: none; 
}