简体   繁体   English

如何使用jQuery禁用选择框中的非选定选项?

[英]How to disable non selected options in select boxes using jQuery?

I have a model in Laravel that can be edited with dynamic form but i want to prevent changing the selected option in select box so when i open the edit page i should be able to see the select boxes with selected items but all other options should be disabled. 我在Laravel中有一个可以用动态表单编辑的模型,但我想阻止更改选择框中的选定选项,所以当我打开编辑页面时,我应该能够看到包含所选项目的选择框,但所有其他选项应该是禁用。

Maybe i could just select all select with jQuery and disable all non selected values?? 也许我可以用jQuery选择所有select并禁用所有未选择的值? How to do that? 怎么做?

I have tried this: 我试过这个:

// disable non selected items
$('select').each(function(){
    $('option').each(function() {
        if(this.selected) {
            this.attr('disabled', true);
        }
    });
});

But it doesn't work, i just need a little help 但它不起作用,我只需要一些帮助

this is the select box and all others are the same: 这是选择框,其他所有都是相同的:

<select name="car">
  <option selected="selected" value="1">Volvo</option>
  <option value="2">Saab</option>
  <option value="3">Mercedes</option>
  <option value="4">Audi</option>
</select>

just other values selected 只选择其他值

Your code has two problems. 您的代码有两个问题。

First : you should check if option isn't selected ( !this.selected ) then disable it. 首先 :您应该检查是否未选择选项( !this.selected )然后禁用它。

Second : this.attr('disabled', true) doesn't work in jQuery. 第二this.attr('disabled', true)在jQuery中不起作用。 You should use $(this) instead. 你应该使用$(this)代替。

$('select').each(function(){
    $('option').each(function() {
        if(!this.selected) {
            $(this).attr('disabled', true);
        }
    });
});

 $('select').each(function(){ $('option').each(function() { if(!this.selected) { $(this).attr('disabled', true); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="car"> <option selected="selected" value="1">Volvo</option> <option value="2">Saab</option> <option value="3">Mercedes</option> <option value="4">Audi</option> </select> 

Note that first .each() loop in you code is additional. 请注意,代码中的第一个.each()循环是额外的。 The bottom code is enough. 底部代码就足够了。

$('option').each(function() {
  !this.selected ? $(this).attr('disabled', true) : "";
});
   $(function(){
        var Obj=$('select').find('option');
          $.each(Obj,function(){
           $(this).is(":selected") ? "" :$(this).attr('disabled',true);
          });
     });

You can replace "this" by "$(this)" and add "!" 您可以用“$(this)”替换“this”并添加“!” in the if and it can work: 在if和它可以工作:

$('select').each(function(){
    $('option').each(function() {
        if(!$(this).selected) {
            $(this).attr('disabled', true);
        }
    });
});

Ok I am not really sure what you want to do, but I can fix your first code snippet for you: 好的,我不确定你想做什么,但我可以为你修复你的第一个代码片段:

// disable non selected items
$('select').each(function(){
    $('option').each(function() {
        if(!this.selected) {
            $(this).parent().attr('disabled', true);
        }
    });
});

This is just an update for others: 这只是其他人的更新:

I have found (for me even better solution) https://silviomoreto.github.io/bootstrap-select/examples/#disabled-select-box 我找到了(对我来说更好的解决方案) https://silviomoreto.github.io/bootstrap-select/examples/#disabled-select-box

with this plugin you can set the select box to disabled and then you can see what was previously selected and you can't change it. 使用此插件,您可以将选择框设置为禁用,然后您可以看到之前选择的内容,您无法更改它。

Just load the plugin in your view and add class .selectpicker and set the select box to disabled and it looks like this: 只需在视图中加载插件并添加类.selectpicker并将选择框设置为禁用,它看起来像这样:

在此输入图像描述

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

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