的javascript + div标签

问题描述:

这些天我已阅读并了解更多关于我的问题的代码是在这里:的javascript + div标签

<div align="right" id="parent" name="parent"> 
<select name="select30" id="select30" value=""/>here inside i have options values and work dynamically with query to my DB</select> 
<input type="button" id="moreFields" value="+" onclick=""/> //add select tags 
<input type="button" value="-" onclick="" /> //remove select tags 
<div name="child" id="writeclone"></div> //here cloned the child from parent DIV 
</div> 
<input type="button" name="enter" id="" value="ENTER" onclick="getoptionvalues();"/> 

我的问题是我怎么能得到的名称或ID +按钮fired.When当从孩子DIV的此按钮激发儿童DIV创建子的DIV!有人可以帮我改正我的JavaScript代码

<script> 
function getoptionvalues() { 
    var parent=document.getElementById('parent'); 
    for (var count=0;count<parent.childNodes.length;count++) { 
     if(parent.childNodes[count].tagName =='DIV') { 
      alert ('parent.childNodes[count]'); 
     } 
    } 
} 
</script> 
+3

'警报( 'parent.childNodes [COUNT]');'应'警报(parent.childNodes [COUNT] );' – ThiefMaster 2011-05-06 13:28:07

由于ThiefMaster指出,'parent.childNodes[count]'应该parent.childNodes[count]。然后拿到ID,它只是.id和名称是.name

if(parent.childNodes[count].tagName =='DIV') { 
     alert (parent.childNodes[count].id); 
     alert (parent.childNodes[count].name); 
    } 

最起码,你需要一个方法的名称添加到您的onClick:

<input type="button" id="moreFields" value="+" onclick="$:getoptionvalues()"/> 

然后,使用jQuery,你可以抓住某种类型的组件,然后循环的阵列至w /警报:

function getoptionvalues() { 
    var dropdownMenus = $("select"); 
    dropdownMens.each(function() { 
     var id = $(this).id; 
     alert(id); 
    }); 
} 
+0

正如无处不在的jQuery一样,它不是问题的一部分。另外'onclick =“getoptionvalues()”'应该工作得很好。你为什么说他需要'$:'? – Jeff 2011-05-06 13:39:21

+0

你可能是对的。我最近写了太多的MVC,并且我认为$:在这种情况下是必须的。 – WEFX 2011-05-06 13:45:05

+0

再次感谢您的答复,但是没有任何事情发生,当我按下按钮时,您能看到请您的jquery代码吗?如果您想了解我的代码的更多细节,请告诉我!我一直阅读并在论坛上看,但没有解决方案! – tasox 2011-05-11 08:44:50