JavaScript獲取相同id的元素


在servlet里,你可以直接使用

1 String[] property=request.getParameterValues("property");//屬性

來獲取相同id的屬性值。但是在js里,這是行不通的。我們只有采用尋找結點的方式來獲取元素。使用這種方法的話,id是否相同對我們來說已經是透明的了。

 1   property[0]=$("#property").val();
 2    relation[0]=$("#relation").val();
 3    propertyvalue[0]=$("#wordvalue").val();
 4    node=$("#property").parent().parent().next();
 5    while(node.length>0){
 6        property[i]=node.children(":first").children(":first").val();
 7        relation[i]=node.children(":first").next().children(":first").val();
 8        propertyvalue[i]=node.children(":first").next().next().children(":first").val();
 9        node=node.next();
10        i++;
11    }

上面的例子中,設置三個數組變量:

1 var property=new Array();
2 var relation=new Array();
3 var propertyvalue=new Array();

其中,parent()返回的是該結點的父節點(只返回一個結點)。

children()返回的是當前結點的所有子結點,如果直接使用property=node.children().val();會出錯。如使用children(":first"),則返回的是第一個子節點。

next()返回的是當前結點的下一個兄弟結點。

node.length>0是用來判斷當前結點是否存在的。

這樣就可以遍歷所有想要的結點。

 

1 $.post("test/ContextServlet",{property:property,relation:relation,
2             propertyvalue:propertyvalue},function(data){autoNode.html(data);});

上面使用jq來向servlet傳值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM