我的目的是获得具有相同类别的所有元素的id和值,但是将其推送到数组中并不适用于我。 任何帮助表示赞赏。 下面是我的javascript函数:
content1是具有多个div(用第一个div克隆)的类,点击按钮创建。 但我的数组长度始终为零。
var newList = [];
function Intfdata(){
var numItems = $('.content1').length;
alert(numItems);
$.each($('.content1 *[id]'), function(index, value) {
var currentId=($(value).attr('id'));
var currentVal= (this.value);
alert(currentId +" and " + currentVal );
newList.push = ({currentId, currentVal});
});
alert(newList.length);
};
HTML如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.1/processing-api.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="file:///C:/Users/sp0065506/Desktop/xml generation/RRICdatascript_MI.js"></script>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>Router Interface data generation</title>
<h1>Router Interface data generation</h1>
</head>
<body>
<div id="gen_fields">
<div class="content">
<span>Customer: <input type="text" id="Customer" style="width:48px;" name="Customer[]" value="" /></span>
<span>CountryCode: <input type="text" id="CountryCode" style="width:48px;" name="CountryCode[]" value="" /></span>
<span>HostName: <input type="text" id="HostName" style="width:200px;" name="HostName[]" value="" /></span>
<span>InterfaceName: <input type="text" id="InterfaceName" style="width:200px;" name="InterfaceName[]" value="" /></span>
</div>
<div>
<span>ManagementIP: <input type="text" id="ManagementIP" style="width:100px;" name="ManagementIP[]" value="" /></span>
<span>SubnetMask: <input type="text" id="SubnetMask" style="width:100px;" name="SubnetMask[]" value="" /></span>
<span>IPMode: <select id="IPMode" name="IPMode[]" value="" /></span>
<option value="0">IPv4</option>
<option value="2">IPv6</option>
<option value="1">Both</option>
</select>
</div>
<button id="InterfaceButton" name="InterfaceButton[]" value="" onclick="duplicate();">Add Interface</button>
<div class="content1" id="intffields" value="">
<div class='label'>InterfaceList:</div>
<span>Action: <select id="Action" name="Action[]" value="" /></span>
<option value="A">A</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<span>PortName: <input type="text" id="PortName" style="width:200px;" name="PortName[]" value="" /></span>
<span>PortType: <input type="text" id="PortType" style="width:200px;" name="PortType[]" value="" /></span>
<span>IPAddress: <input type="text" id="IPAddress" style="width:200px;" name="IPAddress[]" value="" /></span>
<span>SubnetMask: <input type="text" id="SubnetMask1" style="width:200px;" name="SubnetMask1[]" value="" /></span>
<div>
<span>IPv6Address: <input type="text" id="IPV6Address" style="width:200px;" name="IPV6Address[]" value="" /></span>
<span>IPv6Length: <input type="text" id="IPV6Length" style="width:50px;" name="IPV6Length[]" value="" /></span>
<span>PortLevelInterface:<select id="PortLevelInterface" name="PortLevelInterface[]" value="">/span>
<option value=" "></option>
<option value="Y">Y</option>
<option value="N">N</option>
</select>
<span>PortLevelCos:<select id="PortLevelCos" name="PortLevelCos[]" value="">/span>
<option value=" "></option>
<option value="Y">Y</option>
<option value="N">N</option>
</select>
<span>PortSpeed: <input type="text" id="PortSpeed" style="width:50px;" name="PortSpeed[]" value="" /></span>
<span>CosMode: <select id="CosMode" name="CosMode[]" value="">/span>
<option value="Reset Mode">None Mode</option>
<option value="Transparent Mode">Transparent Mode</option>
<option value="Passive Mode">Passive Mode</option>
<option value="Provider Mode">Provider Mode</option>
<option value="None Mode">Reset Mode</option>
</select>
<span>LCType: <input type="text" id="LCType" style="width:50px;" name="LCType[]" value="" /></span>
<span>Usage: <select id="Usage" name="Usage[]" value="">/span>
<option value="Network Access">Network Access</option>
<option value="Cascaded Access">Cascaded Access</option>
<option value="Customer Access">Customer Access</option>
<option value="Resiliency Access">Resiliency Access</option>
<option value="Backup Network Access">Backup Network Access</option>
</select>
<span>TCGroup_No: <input type="text" id="TCGroup_No" style="width:50px;" name="TCGroup_No[]" value="" /></span>
<span>COSModel:<select id="COSModel" class="cosm" name="COSModel" value="" onchange="divclick(this);"></span>
<option value=" " selected="selected"></option>
<option value="4COS">4COS</option>
<option value="6COS">6COS</option>
</select>
</div>
<div id="holder"></div>
<div class="content2">
</div>
<div id="4COS" class="style-sub-1" style="display: none;" name="stylesub1" >
<label for="CatchAllClass">CatchAllClass:</label>
<select name="cat" id="cat" class="postform">
<option class="level-0" value="COS1">COS1</option>
<option class="level-0" value="COS2">COS2</option>
<option class="level-0" value="COS3">COS3</option>
<option class="level-0" value="COS4">COS4</option>
</select>
<label for="FragmentClass">FragmentClass:</label>
<select name="cat1" id="cat1" class="postform">
<option class="level-0" value="COS1">COS1</option>
<option class="level-0" value="COS2">COS2</option>
<option class="level-0" value="COS3">COS3</option>
<option class="level-0" value="COS4">COS4</option>
</select>
</div>
<div id="6COS" class="style-sub-1" style="display: none ;" name="stylesub1" >
<label for="CatchAllClass">CatchAllClass:</label>
<select name="cat" id="cat" class="postform">
<option class="level-0" value="COS1">COS1</option>
<option class="level-0" value="COS2">COS2</option>
<option class="level-0" value="COS2V">COS2V</option>
<option class="level-0" value="COS3">COS3</option>
<option class="level-0" value="COS4">COS4</option>
<option class="level-0" value="COS5">COS5</option>
</select>
<label for="FragmentClass">FragmentClass:</label>
<select name="cat1" id="cat1" class="postform">
<option class="level-0" value="COS1">COS1</option>
<option class="level-0" value="COS2">COS2</option>
<option class="level-0" value="COS2V">COS2V</option>
<option class="level-0" value="COS3">COS3</option>
<option class="level-0" value="COS4">COS4</option>
<option class="level-0" value="COS5">COS5</option>
</select>
</div>
</div>
</div>
<div id="download">
<button id="DownloadButton">Create file</button>
<div>
<div id="generated" style="display:none">
<h2>RouterInterfaceData.xml</h2>
<textarea id="ResultXml" style="width: 100%; height: 100em" readonly="readonly"></textarea>
</div>
</body>
</html>
答案 0 :(得分:0)
您似乎误解了push
的工作原理以及对象的结构。
您正在尝试将对象推入数组。该对象应如下所示:
{ currentId: currentId, currentVal: currentVal }
你有许多键/值对。
push
应该像这样使用:
newList.push({ currentId: currentId, currentVal: currentVal });