如何改变输入边框颜色,用jQuery,取决于它的值? [英] How to change input border-color, with jQuery, depending on its value?
本文介绍了如何改变输入边框颜色,用jQuery,取决于它的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有关于jQuery的问题。我有下面的代码工作正常;它根据其输入和输入的值更改输入
border-color
。但是如果在输入
的值在页面加载它不会改变 border-color
。如何在页面加载后从头开始更改 border-color
?
I have question about jQuery. I have code below which works fine; it changes input
border-color
depending on its value, which is typed and entered. But if in input
's value on page-load it does not change that border-color
. How can I make the border-color
change from the beginning, following page-load?
谢谢:)
<input type="text" class="col" value="">
// When the <input>'s value changes
$("input").change(function() {
// If the value is less than 7, add a red border
if ($(this).val() < 7) {
$(this).css("border", "5px solid red");
}
// Else if the value is equal to 7, add a green border
else if ($(this).val() == 7) {
$(this).css("border", "5px solid green");
}
// Else if the value is greater than 7, add an orange border
else if ($(this).val() > 7) {
$(this).css("border", "5px solid orange");
}
// Else if the value is anything else, add a black border
else {
$(this).css("border", "5px solid black");
}
});
推荐答案
只需触发事件:
$("input").change(function() {
// ...
}).trigger("change");
DEMO: http://jsfiddle.net/9B5SX/1/
这篇关于如何改变输入边框颜色,用jQuery,取决于它的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文