Jquery 当文本框为空时如何更改文本框的边框颜色丢失焦点事件和onfocus事件如果文本不为空则不更改边框颜色

Jquery 当文本框为空时如何更改文本框的边框颜色丢失焦点事件和onfocus事件如果文本不为空则不更改边框颜色,jquery,Jquery,如果文本框为空,我想使用事件更改文本框的边框颜色 js $(document).ready(function () { $("#name1").onFocus(function () { if ($("#name1").val() == "") $(this).css({ "border": "red", }); $("#name1").LostFocus(function () { if (

如果文本框为空,我想使用事件更改文本框的边框颜色

js

$(document).ready(function () {
    $("#name1").onFocus(function () {
        if ($("#name1").val() == "") $(this).css({
            "border": "red",
        });
        $("#name1").LostFocus(function () {
            if ($("#name1").val() != "") $(this).css({
                "border": "",
            });
        });
    });
});
.red {
     border-color:red;   
}
HTML

<form action="">
    <input id="name1" type="textbox"></input>
    <input id="name2" type="textbox"></input>
    <input id="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
<form action=""> 
    <input id ="name1" type="textbox"></input>
    <input id ="name2" type="textbox"></input>
    <input id ="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>


尝试
聚焦
而不是
聚焦
,并使用
模糊
进行
失焦

$(document).ready(function() {
$("#name1").focus(function(){
if ($("#name1").val() == "")
$(this).css({
"border": "red",
});

$("#name1").blur(function(){

if ($("#name1").val() != "")
$(this).css({
"border": "",
});


});
});

阅读更多:和尝试
focus
而不是
onFocus
,并使用
blur
进行
LostFocus

$(document).ready(function() {
$("#name1").focus(function(){
if ($("#name1").val() == "")
$(this).css({
"border": "red",
});

$("#name1").blur(function(){

if ($("#name1").val() != "")
$(this).css({
"border": "",
});


});
});

阅读更多:和尝试
focus
而不是
onFocus
,并使用
blur
进行
LostFocus

$(document).ready(function() {
$("#name1").focus(function(){
if ($("#name1").val() == "")
$(this).css({
"border": "red",
});

$("#name1").blur(function(){

if ($("#name1").val() != "")
$(this).css({
"border": "",
});


});
});

阅读更多:和尝试
focus
而不是
onFocus
,并使用
blur
进行
LostFocus

$(document).ready(function() {
$("#name1").focus(function(){
if ($("#name1").val() == "")
$(this).css({
"border": "red",
});

$("#name1").blur(function(){

if ($("#name1").val() != "")
$(this).css({
"border": "",
});


});
});

阅读更多信息:并使用自定义onChange处理程序:


使用自定义onChange处理程序:


使用自定义onChange处理程序:


使用自定义onChange处理程序:


使用模糊尝试此操作

 $("input[type='textbox']").blur(function () {
    var text = $(this).val();
    if(text == "")
    {
        $(this).css('border-color','red');
    }
    });

小提琴:

使用模糊试试这个

 $("input[type='textbox']").blur(function () {
    var text = $(this).val();
    if(text == "")
    {
        $(this).css('border-color','red');
    }
    });

小提琴:

使用模糊试试这个

 $("input[type='textbox']").blur(function () {
    var text = $(this).val();
    if(text == "")
    {
        $(this).css('border-color','red');
    }
    });

小提琴:

使用模糊试试这个

 $("input[type='textbox']").blur(function () {
    var text = $(this).val();
    if(text == "")
    {
        $(this).css('border-color','red');
    }
    });
小提琴:

试试这个

现场观看

HTML

<form action="">
    <input id="name1" type="textbox"></input>
    <input id="name2" type="textbox"></input>
    <input id="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
<form action=""> 
    <input id ="name1" type="textbox"></input>
    <input id ="name2" type="textbox"></input>
    <input id ="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
jQuery

$(document).ready(function() {
    $("input").focusin(function(){
        if ($(this).val() == "")
        $(this).addClass("red")
    });
    $("input").focusout(function(){
        if ($(this).val() != "")
        $(this).removeClass("red")
    });
});
试试这个

现场观看

HTML

<form action="">
    <input id="name1" type="textbox"></input>
    <input id="name2" type="textbox"></input>
    <input id="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
<form action=""> 
    <input id ="name1" type="textbox"></input>
    <input id ="name2" type="textbox"></input>
    <input id ="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
jQuery

$(document).ready(function() {
    $("input").focusin(function(){
        if ($(this).val() == "")
        $(this).addClass("red")
    });
    $("input").focusout(function(){
        if ($(this).val() != "")
        $(this).removeClass("red")
    });
});
试试这个

现场观看

HTML

<form action="">
    <input id="name1" type="textbox"></input>
    <input id="name2" type="textbox"></input>
    <input id="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
<form action=""> 
    <input id ="name1" type="textbox"></input>
    <input id ="name2" type="textbox"></input>
    <input id ="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
jQuery

$(document).ready(function() {
    $("input").focusin(function(){
        if ($(this).val() == "")
        $(this).addClass("red")
    });
    $("input").focusout(function(){
        if ($(this).val() != "")
        $(this).removeClass("red")
    });
});
试试这个

现场观看

HTML

<form action="">
    <input id="name1" type="textbox"></input>
    <input id="name2" type="textbox"></input>
    <input id="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
<form action=""> 
    <input id ="name1" type="textbox"></input>
    <input id ="name2" type="textbox"></input>
    <input id ="name3" type="textbox"></input>
    <input type="button" value="submit"></input>
</form>
jQuery

$(document).ready(function() {
    $("input").focusin(function(){
        if ($(this).val() == "")
        $(this).addClass("red")
    });
    $("input").focusout(function(){
        if ($(this).val() != "")
        $(this).removeClass("red")
    });
});

尝试
focus
而不是
onFocus
,对
LostFocus
使用
blur
。尝试
focus
而不是
onFocus
并对
LostFocus
使用
blur
并对
LostFocus
使用
blur
。尝试
focus对于
LostFocus
使用
blur
,而不是
onFocus