jQuery javascript在下拉值更改时将属性设置为只读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17880998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:08:38  来源:igfitidea点击:

javascript to set attribute readonly on dropdown value change

javascriptjquerydrop-down-menu

提问by L4reds

on selecting values from the drop down, if the selected values equals to a certain value, i want the dropdown to change to readonly, how can i do that?

从下拉列表中选择值时,如果所选值等于某个值,我希望下拉列表更改为只读,我该怎么做?

HTML:

HTML:

<select id="s_id">
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="opel">Opel</option>
   <option value="audi">Audi</option></select>

Script:

脚本:

$(document).ready(function () {
   $("#s_id").change(function () {
     var x = $(this).val();
     //alert("something's changed");
     alert($(this).val());
     if (x === opel) {
         alert("iff only.....");
         $(this).attr("readOnly", "true");
    }
   });
});

Fiddle: http://jsfiddle.net/L4reds/xKQUd/2/

小提琴:http: //jsfiddle.net/L4reds/xKQUd/2/

回答by verbanicm

You need to compare xto a string of "opel"and you can use the attribute disabled, see fiddle here: http://jsfiddle.net/xKQUd/3/

您需要与x字符串进行比较"opel",您可以使用属性disabled,请参阅此处的小提琴:http: //jsfiddle.net/xKQUd/3/

To clarify: selectelements cannot be set to readOnly, need to use the disabledattribute

澄清:select元素不能设置为readOnly,需要使用disabled属性

To get around the not sending to the server issue, set a hidden input equal to the disabled value that will be sent on form submission instead, view this fiddle here: http://jsfiddle.net/xKQUd/25/

要解决不发送到服务器的问题,请将隐藏输入设置为将在表单提交时发送的禁用值,请在此处查看此小提琴:http: //jsfiddle.net/xKQUd/25/

回答by Ishan Jain

In your code, you used opelas a variable. but it's not a variableit's a string. So you need to take as a string (in single or double quotes).

在代码中,你用opelvariable。但它不是一个variable它是一个string。所以你需要把它当作一个字符串(单引号或双引号)。

$(document).ready(function () {
    $("#s_id").change(function () {
        var x = $(this).val();            
        alert($(this).val());
        alert("x=======" +x);
        if (x == "opel") {
            alert("iff");
            $(this).attr("disabled", "disabled");
        }
    });
});

Try this jsfiddle

试试这个 jsfiddle

or take a variable var opel = "opel";.

或取一个变量var opel = "opel";

Try jsfiddle

试试 jsfiddle

回答by melancia

Selectelements can't be set readonly, but can be disabledinstead. But in this case, their value won't be submitted with the form.

Select元素不能被设置readonly,但可以被设置disabled。但在这种情况下,它们的值不会与form.

You have two options here.

您在这里有两个选择。

One is to set a hidden inputwith the selected value, and then disable the select:

一种是hidden input使用选定的值设置一个,然后禁用select

$(document).ready(function () {
   $("#s_id").on("change", function () {
       if ($(this).val() === "opel") {
           $("#myHiddenField").val($(this).val());
           $(this).attr("disabled", "disabled");
       }
   });
});

The other one, is to remove all the other options from the select, leaving just the selected one available:

另一种是从 中删除所有其他选项select,只保留选定的选项:

$(document).ready(function () {
   $("#s_id").on("change", function () {
     if ($(this).val() === "opel") {
         $(this).children("option").each(function() {
            if (this.value != "opel")
               this.remove();
         });
     }
   });
});

It's up to you now.

现在就看你了。

回答by Elen

Selectdoesn't have attr ReadOnly- instead it can be disabledso your code:

Select没有 attr ReadOnly- 相反它可以是disabled你的代码:

$(document).ready(function () {
    $("#s_id").change(function () {
        var x = $(this).val();
        //alert("something's changed");
        alert($(this).val());
        alert("x=======" +x);
        if (x == "opel") {
            alert("iff");
           // $(this).attr("readOnly", "true");
            $(this).attr('disabled',true);
        }
    });
});

btw openis not a variable, but value, so needs to be in quotes "opel"and fiddle: http://jsfiddle.net/xKQUd/5/

顺便说一句open,不是变量,而是值,所以需要用引号引起来"opel"http: //jsfiddle.net/xKQUd/5/

回答by Neeraj Dubey

Try

尝试

if (x == 'opel') {
        alert("iff");
        $(this).attr("disabled", "true");
    }

FIDDLE Demo

小提琴演示

回答by Jeet Bhatt

Try this,

尝试这个,

if (x == "opel") {

            $(this).attr("disabled", "disabled");
        }

回答by Nitin Chaurasia

Try Below Code:

试试下面的代码:

<select id="s_id">
 <option value="volvo">Volvo</option>
 <option value="saab">Saab</option>
 <option value="opel">Opel</option>
 <option value="audi">Audi</option>
 </select>

Jquery Code:

查询代码:

$(document).ready(function () {
$("#s_id").change(function () {
 var x = $(this).val();    
 if (x == 'opel') {
     $(this).attr('disabled','disabled');
}
});
});

http://jsfiddle.net/Ea6NN/

http://jsfiddle.net/Ea6NN/

回答by Four_lo

Here you are http://jsfiddle.net/SteveRobertson/xKQUd/24/

你在这里http://jsfiddle.net/SteveRobertson/xKQUd/24/

      $(document).ready(function () {
          y=1;
          $("#s_id").change(function () {
             var x = $(this).val();
    //alert("something's changed");


             if (x == "opel" && x!=y) {           
                y=x;
                $("#s_id").prop("disabled", true).change();            
    }
});

});

});