简体   繁体   English

jQuery<select>选项仅启用一些选项

[英]jQuery <select> option enable just some options

How do I choose which one selector/option I want to disable/enable while selecting one of options at other selectors如何在选择其他选择器的选项之一时选择要禁用/启用的选择器/选项

For example:例如:

I would like to have them all disabled except for "unit_block", after I choose one of the option (A or B) "unit_row_big" will enable allowing me to chose any of the options, but "unit_row" will enable only some of the options.我希望除了“unit_block”之外的所有选项都被禁用,在我选择一个选项(A 或 B)之后,“unit_row_big”将允许我选择任何选项,但“unit_row”将仅启用其中的一些选项。

Basically here is spreadsheet how I would like to create it.基本上这里是电子表格,我想如何创建它。

I have everythink working basically, but it is not bulletproof.我有everythink 基本工作,但它不是防弹。

How you can see in block A (upper one) there are 4 bigger cells (unit_row_big) wile block B have 3 of them.您如何在块 A(上部)中看到有 4 个较大的单元格 (unit_row_big),而块 B 有 3 个。 Then some bigger cells in block B have 12 smaller cells (unit_row) but all of them have 5 smaller cells wide (unit_column).然后块 B 中一些较大的单元格有 12 个较小的单元格 (unit_row),但它们都有 5 个较小的单元格宽 (unit_column)。 For example if I would like to create Large (unit_size) cell on 2-5th position it would collide with next big cell so I would like to have exception that size Large can be used only when unit_column is 1 Medium if unit_column is 1 or 5 and Small every time.例如,如果我想在第 2-5 个位置创建大 (unit_size) 单元格,它会与下一个大单元格发生碰撞,所以我想有一个例外,即如果 unit_column 为 1 或 5,则仅当 unit_column 为 1 Medium 时才可以使用 size Large和小每次。

I found few examples of disabling/enabling option in selectors but none this particular.我发现在选择器中禁用/启用选项的例子很少,但没有这个特别的。 Since I'm pretty bad with javascripts I have no idea how to create one, and I feel good if I can at least edit one.由于我对 javascripts 非常糟糕,我不知道如何创建一个,如果我至少可以编辑一个,我感觉很好。

Spreadsheet 电子表格

JSFiddle demo JSFiddle 演示

    <div class="form-group">
    <select class="selectpicker" name="unit_block" id="unit_block" >
    <option>A</option>
    <option>B</option>
    </select>
    </div>

    <div class="form-group">
    <select class="selectpicker" name="unit_row_big" id="unit_row_big" disable>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select>
    </div>

    <div class="form-group">
    <select class="selectpicker" name="unit_row" id="unit_row">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    </select>
    </div>

    <div class="form-group">
    <select class="selectpicker" name="unit_column" id="unit_column">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    </select>
    </div>


    <div class="form-group">
    <select class="selectpicker" name="unit_size" id="unit_size">                
    <option>Small</option>
    <option>Medium</option>
    <option>Large</option>
    </select>
    </div>

You could use jQuery but this is pretty easy in pure JavaScript.您可以使用 jQuery,但这在纯 JavaScript 中非常简单。

You just need to monitor whatever selectors you want and then make the disabled attribute true based on those choices.您只需要监控您想要的任何选择器,然后根据这些选择使disabled属性为true I made a small fiddle where if B is selected in the first dropdown, 2 is disabled on the second dropdown.我做了一个小提琴其中如果B在第一下拉选择, 2是在第二下拉禁用。 You should be able to easily expand on that.您应该能够轻松地扩展它。

基于其他下拉菜单的禁用选项。

https://jsfiddle.net/ryanpcmcquen/6no3jyzb/ https://jsfiddle.net/ryanpcmcquen/6no3jyzb/

 document.addEventListener('DOMContentLoaded', function () { 'use strict'; var unitBlock = document.querySelector('select#unit_block'); var unitRowBig = document.querySelector('select#unit_row_big'); unitBlock.addEventListener('change', function () { if (unitBlock.value === 'B') { // [1] is equal to option '2' unitRowBig[1].disabled = true; } else { unitRowBig[1].disabled = false; } }); });
 <form class="form-signin" method="post" id="register-form"> <h2 class="form-signin-heading">Unit specification</h2> <hr /> <div id="error"> <!-- error will be showen here ! --> </div> <div class="form-group"> <input type="text" class="form-control" name="unit_name" id="unit_name" /> </div> <div class="form-group"> <input type="text" class="form-control" name="unit_group" id="unit_group" /> </div> <div class="form-group"> <select class="selectpicker" name="unit_block" id="unit_block"> <option>A</option> <option>B</option> </select> </div> <div class="form-group"> <select class="selectpicker" name="unit_row_big" id="unit_row_big"> <option>1</option> <option>2</option> <option>3</option> </select> </div> <div class="form-group"> <select class="selectpicker" name="unit_row" id="unit_row"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> <option>11</option> <option>12</option> </select> </div> <div class="form-group"> <select class="selectpicker" name="unit_column" id="unit_column"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="form-group"> <select class="selectpicker" name="unit_size" id="unit_size"> <option>Small</option> <option>Medium</option> <option>Large</option> </select> </div> <hr /> <div class="form-group text-center"> <button type="submit" class="btn btn-default" name="btn-save" id="btn-submit">Insert</button> </div> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM