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