質問

I have 'button' like this

<span data-id="dr21" data-minheight="100" data-maxheight="200" data-minwidth="20" data-maxwidth="50"  class="customsizebutton">(edit size)/span>

and script like this

<script>
$('.customsizebutton').click(function ()
{
var id = $(this).data('id');    
var minHeight = $(this).data('minheight');
var maxHeight = $(this).data('maxheight');
var minWidth = $(this).data('minwidth');
var maxWidth = $(this).data('maxwidth');
var arrayH = [];
var arrayW = [];
for (var i = minHeight; i <= maxHeight-1; i++) {
arrayH.push(i);
}
for (var i = minWidth; i <= maxWidth-1; i++) {
arrayW.push(i);
}

var selectListH = document.getElementById("h-"+id);
for (var i = 0; i < arrayH.length; i++) {
var option = document.createElement("option");
 option.text = arrayH[i];
selectListH.appendChild(option);
}

var selectListW = document.getElementById("w-"+id);

for (var i = 0; i < arrayW.length; i++) {
var option = document.createElement("option");
 option.text = arrayW[i];
selectListW.appendChild(option);
}})
</script>

I am trying to fill the two dropdowns with

<option>200</option>
<option>199</option>...    
<option>101</option>
<option>100</option>

<option>50</option>
<option>49</option>...    
<option>21</option>
<option>20</option>

It currently fills the dropdowns the opposite direction (low to high). I'm new to this and trial and error has got me this far.

Thanks

正しい解決策はありません

他のヒント

Just populate the arrays in reverse order.

for (var i = maxHeight-1; i >= minHeight; i--) {
arrayH.push(i);
}
for (var i = maxWidth-1; i >= minWidth; i--) {
arrayW.push(i);
}

If you merely need to populate your dropdowns in reverse order, you can just iterate them in reverse, like so:

for (var i = arrayH.length - 1; i >= 0; i--) {
var option = document.createElement("option");
 option.text = arrayH[i];
selectListH.appendChild(option);
}

If you want to actually reverse the items in the array, as the title of your question denotes, that's even easier, with the Array.reverse function:

arrayH = arrayH.reverse();

If you're new, I recommend reviewing the JavaScript documentation over at Mozilla.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top