2011-05-06 88 views
0

這些天我已閱讀並瞭解更多關於我的問題的代碼是在這裏:的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

回答

2

由於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); 
    } 
0

最起碼,你需要一個方法的名稱添加到您的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