简体   繁体   English

如何在删除项目时保持选择框的宽度不变?

[英]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.

相关问题 如何在ReactJS中添加和删除选择框列表 - How can I add and remove list of select box in reactjs 如何使用javascript和css从选择框选项中删除焦点 - How can I remove focus from select box Options using javascript and css 从选择框中删除项目或将项目添加到选择框中? - Remove items from or add items to a select box? 如何从 angularjs select 下拉列表中删除空/空项目? - How can i remove empty/null items from angularjs select dropdown? 如何在使用jquery更改选择值时调用选择框? - how can i call select box on change while changing select value using jquery? 当所有 select 框都有一个名称时,我如何从 DOM 添加/删除 select 框,以便在提交时我从活动的 select 框中获取值? - How can I add/remove select boxes from DOM when all select boxes have one name so that upon Submit I get value from active select box? 如何从 object 中删除属性但保留其值? - How can i remove a property from an object but keep its values? 我如何在数据网格中排列具有相等宽度的选择框 - how can i arrange a select box with the equal width of field,which is in data grid 如何从不断变化的幻灯片中删除闪光灯? - How can I remove a flash from a changing slideshow? 如何使用LocalStorage从待办事项列表中删除项目 - How can i remove items from Todo List with LocalStorage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM