[英]How can I keep the width of a select box from changing as I remove items?
I have two SELECT boxes on a page I am working on. 我正在处理的页面上有两个SELECT框。 Selecting a value in the first box causes the options available in the second box to be filtered, and I keep an empty option on top of both boxes.
在第一个框中选择一个值会导致第二个框中可用的选项被过滤,而我在两个框上方都保留了一个空选项。 When the selected option of the first box is the empty box, I show all of the options in the second box.
当第一个框的选定选项为空框时,我将在第二个框中显示所有选项。
The issue is that both boxes are in a flexbox container with additional fields to the right. 问题是两个盒子都在flexbox容器中,右边还有其他字段。 Whenever I change the selected option in the first box, the width of the second box changes and then all of the fields to the right are moved around.
每当我在第一个框中更改选定的选项时,第二个框的宽度就会更改,然后右边的所有字段都会移动。 What I need to be able to do is keep the second box from shrinking when items are filtered out when I change the selection of the first box.
我需要做的是,当我更改第一个框的选择时,当项目被滤除时,第二个框就不会收缩。 I want to be able to set the min-width CSS property of the second select box but since the starting width is based on the content and not set in CSS, I can't use $('#second-box').width() to set the min-width property since it returns 0.
我希望能够设置第二个选择框的最小宽度CSS属性,但是由于起始宽度基于内容且未在CSS中设置,因此我不能使用$('#second-box')。width ()设置min-width属性,因为它返回0。
How can I go about determining the content-based width of the second box in javascript to prevent the resizing? 如何确定JavaScript中第二个框的基于内容的宽度以防止调整大小?
<div class="VerticalField">
<label for="FirstBox" id="lblFirstBox" class="FormLabel VerticalLabel">First Box</label>
<select id="FirstBox" class="FormTextBox VerticalTextBox" onchange="filterSecondBox()">
<option value=""></option>
<option value="51">Option 51</option>
<option value="56">Option 56</option>
<option value="63">Option 63</option>
<option value="65">Option 65</option>
</select>
</div>
<div class="VerticalField">
<label for="SecondBox" id="lblSecondBox" class="FormLabel VerticalLabel">Second Box</label>
<select id="SecondBox" class="FormTextBox VerticalTextBox">
<option value=""></option>
<option value="5101">Sub Option 5101</option>
<option value="5601">Sub Option 5601</option>
<option value="5602">Sub Option 5602</option>
<option value="6301">Sub Option 6301</option>
<option value="6302">Sub Option 6302</option>
<option value="6303">Sub Option 6303</option>
<option value="6501">Sub Option 6501</option>
<option value="6502">Sub Option 6502</option>
<option value="6503">Sub Option 6503</option>
</select>
</div>
And this is my Javascript: 这是我的Javascript:
$('#SecondBox').css('minWidth', $('#SecondBox').width()));
function filterSecondBox()
{
if ($('#FirstBox').data('options') === undefined)
{
$('#FirstBox').data('options', $('#SecondBox option').clone());
}
var id = $('#FirstBox').val();
var options;
if (id == "")
{
options = $('#FirstBox').data('options');
$('#SecondBox').html(options);
}
else
{
options = $('#FirstBox').data('options').filter('[value^=' + id + ']');
$('#SecondBox').html(options);
$('#SecondBox').prepend($('#FirstBox').data('options').first());
}
$('#SecondBox').val('');
}
filterSecondBox();
Clearly "$('#SecondBox').css('minWidth', $('#SecondBox').width());" 显然是“ $('#SecondBox')。css('minWidth',$('#SecondBox')。width());“ doesn't work as the width isn't set so I'm not sure how to determine what the content-based width is to set the minWidth property with.
由于未设置宽度,因此无法正常工作,因此我不确定如何确定设置minWidth属性时基于内容的宽度。
将第二个选择的CSS宽度属性设置为固定宽度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.