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
javascript to set attribute readonly on dropdown value change
提问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");
}
});
});
回答by verbanicm
You need to compare x
to 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: select
elements cannot be set to readOnly
, need to use the disabled
attribute
澄清: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 opel
as a variable
. but it's not a variable
it's a string
. So you need to take as a string (in single or double quotes).
在代码中,你用opel
的variable
。但它不是一个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");
}
});
});
or take a variable var opel = "opel";
.
或取一个变量var opel = "opel";
。
回答by melancia
Select
elements can't be set readonly
, but can be disabled
instead. 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 input
with 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
Select
doesn't have attr ReadOnly
- instead it can be disabled
so 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 open
is 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
回答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');
}
});
});
回答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();
}
});
});
});