用jQuery设置边框颜色 [英] Setting border colour with jquery

查看:1903
本文介绍了用jQuery设置边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html页面中有一个带有单个文本框的简单表单.我希望如果我单击提交按钮时文本框中没有任何值,那么文本框的边框颜色应该是红色.代码

I have a simple form in a html page with a single text box.I want that if I click the submit button with out any value in the text box then the border color of the text box shuld be red.Here is my code

$(document).ready(function(){
  $('.ip').click(function(){
    var optionSelected = false;
    if($('#test').val().length == 0 ) {
      $("#test").css("border-color","red"); 
    }
  });
});

这是表格

<form action="" id="form1">
Test:<input type="text" name="test" id="test">
<input type="submit" class ="ip">
</form>

但是发生的是,如果我单击文本框中没有任何值的提交"按钮,则文本框的边框颜色会暂时设置为红色.红色边框不会持续存在于文本框中

But what is happening is that if I click the submit button without any value in the text box then the border color of the text box is set to red momentarily.The red boder is not persisting in the text box

推荐答案

使用return falsecss("border","1px solid red")代替css("border-color","red")

$(document).ready(function(){
  $('.ip').click(function(){
    var optionSelected = false;
    if($('#test').val().length == 0 ) {
      $("#test").css("border","1px solid red");//more efficient
      return false;
    }
  });
});

演示

这篇关于用jQuery设置边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆