我有以下选择:
<select id="custom-headers" multiple="multiple" class="searchable custom-headers">
<option data-invoice-number="36655" value="1187">RAPCY3</option>
<option data-invoice-number="87172" value="1188">1188</option>
<option data-invoice-number="79190" value="1189">CXETHD</option>
<option data-invoice-number="24584" value="1190">ZDAC2X</option>
<option data-invoice-number="49694" value="1191">2T8SRJ</option>
<option data-invoice-number="11290" value="1192">XDCH5J</option>
<option data-invoice-number="96188" value="1193">83EYS8</option>
<option data-invoice-number="33819" value="1194">WE5PEW</option>
<option data-invoice-number="56529" value="1195">CJEQWM</option>
<option data-invoice-number="55643" value="1196">RAPCY3</option>
<option data-invoice-number="72334" value="1197">1197</option>
<option data-invoice-number="14563" value="1198">CXETHD</option>
<option data-invoice-number="58963" value="1199">ZDAC2X</option>
<option data-invoice-number="44810" value="1200">2T8SRJ</option>
<option data-invoice-number="88482" value="1201">XDCH5J</option>
<option data-invoice-number="9731" value="1202">83EYS8</option>
<option data-invoice-number="41170" value="1203">WE5PEW</option>
<option data-invoice-number="1911" value="1204">CJEQWM</option>
</select>
我需要将值和发票号码收集到一个数组中。 jQuery仅适用于Reslist:
o.reservations = [];
$('.custom-headers option:selected').each(function (i, selected) {
o.reservations[i] = $(selected).val();
});
给我一些1187, 1188, 1189
的数组我需要像
reslist => 1187, 1188, 1189, invoices => 36655, 87172, 79190
尝试:
o.reservations = [];
$('.custom-headers option:selected').each(function (i, selected) {
o.reservations[i] = $(selected).val();
o.reservations[i] = $(selected).data('invoice-number');
});
不确定如何在js中定义键?
答案 0 :(得分:1)
$('.custom-headers option:selected').each(function (i, selected) {
o.reservations.push({
value: $(selected).val(),
number: $(selected).data('invoice-number')
});
});
这将填充以下数组:
[{value: 1187, number: 36655}, {value: 1188, number: 87172}, ...]
答案 1 :(得分:0)
var arr = {"reslist":[], "invoices":[]};
$('.custom-headers option:selected').each(function (i, selected) {
arr.reslist.push($(this).val());
arr.invoices.push($(this).data("invoice-number"));
});
答案 2 :(得分:0)
使用地图。
console.log($('#custom-headers option').map(function(option){
var $this = $(option);
return { invoice-number: $this.data('invoice-number'), value: $this.val() };
}));