我需要JavaScript数组中以下TD的值,稍后我可以在我的代码的另一部分中循环:
<table>
<tr><td class="drdsclient">DSCDS201020101</td></tr>
<tr class="drcheck">
<td>Maximum online generations:</td>
<td class="maxonlval notopborder">30</td>
<td class="maxonlnotes notopborder">notes</td>
</tr>
<tr class="drcheck">
<td>Default Retention Rule set:</td>
<td class="defretruleval notopborder">No default Retention rule set</td>
<td class="defretrulenotes notopborder">notes</td>
</tr>
<tr class="drcheck">
<td>Default Backup Schedule set:</td>
<td class="defschedval notopborder">No default Schedule set</td>
<td class="defschednotes notopborder">notes</td>
</tr>
<tr class="drcheck"> <td class="drdsclient">DSCDS901999102</td></tr>
<tr class="drcheck">
<td>Maximum online generations:</td>
<td class="maxonlval notopborder">29</td>
<td class="maxonlnotes notopborder">notes</td>
</tr>
<tr class="drcheck">
<td>Default Retention Rule set:</td>
<td class="defretruleval notopborder">30 days</td>
<td class="defretnotes notopborder">notes</td>
</tr>
<tr class="drcheck">
<td>Default Backup Schedule set:</td>
<td class="defschedval notopborder">Monday to Friday @ 21:00</td>
<td class="defschednotes notopborder">notes</td>
</tr>
</table>
尝试了下面的JS代码:
var array = $('.drdsclient').map(function() {
var $client = $(this);
var $rows = $client.closest('tr').nextUntil('tr:has(.drdsclient)');
var obj = {
drdsc: $client.text()
};
$rows.each(function() {
obj['maxonlval'] = $('.drcheck').find('td.maxonlval').text();
obj['defretruleval'] = $('.drcheck').find('td.defretruleval').text();
obj['defschedval'] = $('.drcheck').find('td.defschedval').text();
});
return obj;
}).get();
但是对象是重复的:
[0]
[drdsc] => "DSCDS201020101"
[maxonlval] => "3029"
[defretruleval] => "No default Retention rule set30 days"
[defschedval] => "No default Schedule setMonday to Friday @ 21:00"
[1]
[drdsc] => "DSCDS901999102"
[maxonlval] => "3029"
[defretruleval] => "No default Retention rule set30 days"
[defschedval] => "No default Schedule setMonday to Friday @ 21:00"
理想情况下,如果我可以拥有以下内容:
[0]
[drdsc] => "DSCDS201020101"
[maxonlval] => "30"
[defretruleval] => "No default Retention rule set"
[defschedval] => "No default Schedule set"
[1]
[drdsc] => "DSCDS901999102"
[maxonlval] => "29"
[defretruleval] => "30 days"
[defschedval] => "Monday to Friday @ 21:00"
答案 0 :(得分:2)
也许替换这部分
$rows.each(function() {
obj['maxonlval'] = $('.drcheck').find('td.maxonlval').text();
obj['defretruleval'] = $('.drcheck').find('td.defretruleval').text();
obj['defschedval'] = $('.drcheck').find('td.defschedval').text();
});
带
obj['maxonlval'] = $rows.find('td.maxonlval').text();
obj['defretruleval'] = $rows.find('td.defretruleval').text();
obj['defschedval'] = $rows.find('td.defschedval').text();
请注意$rows