更改使用jQuery按值选择标记边框颜色

时间:2017-02-17 14:17:08

标签: javascript jquery html

课程中选择字段的颜色"表格"单击时不要更改为红色,并且不选择选项而保持原样。它应检查该选项是否具有1以外的值,并且边框应保持不变。

.buttonTopdayCSS {
background-color: #a7cfdf; background-image: -webkit-gradient(linear, left top, left bottom, from(#a7cfdf), to(#23538a));
background-image: -webkit-linear-gradient(top, #a7cfdf, #23538a);
background-image: -moz-linear-gradient(top, #a7cfdf, #23538a);
background-image: -ms-linear-gradient(top, #a7cfdf, #23538a);
background-image: -o-linear-gradient(top, #a7cfdf, #23538a);
background-image: linear-gradient(to bottom, #a7cfdf, #23538a);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#a7cfdf, endColorstr=#23538a);
}

.buttonTopdayCSS .x-btn-inner {
font-size: 10px !important;
}

此处$(".Wrapper .Right_Section .Form select").on('blur',function(){ if( $(this, 'option:selected').val() == 0 ){ $(this).css('border-color', '1px solid #C80000'); } else if ($(this).val() > 0) { $(this).css('border-color', '1px solid #BDC7BC'); } }); 是div在彼此之内且select标签位于.Wrapper .Right_Section .Form

4 个答案:

答案 0 :(得分:3)

请检查一下。 删除border-color并添加border,因为您指定了所有边框属性。

$(document).ready(function(){
  $("select").blur(function(){
    //alert($(this).val());
    if(parseInt($(this).val())<=0){
      $(this).css("border","solid 1px red");
    }
    else{
      $(this).css("border","solid 1px blue");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>

答案 1 :(得分:1)

您的代码有两个问题。

  1. jQuery(selector [, context ])中,第一个参数是选择器,第二个参数是上下文。

  2. 您应该在.css()的第二个参数中写入颜色字符串。

  3. $(".Wrapper .Right_Section .Form select").on('blur',function(){ 
      if ( $('option:selected', this).val() == 0 ){
        $(this).css('border-color', '#C80000');     
      }
      else if ($(this).val() > 0) {
        $(this).css('border-color', '#BDC7BC');    
      }
    });
    

    $("select").on('blur',function(){ 
      if ($('option:selected', this).val() == 0)
        $(this).css('border-color', '#C80000');     
      else if ($(this).val() > 0)
        $(this).css('border-color', '#BDC7BC');   
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select>
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

答案 2 :(得分:0)

你必须检查$(this).val() != ""这是第一件事,其次你必须调整你的选择器$(".Wrapper .Right_Section .Form select")也许可以使用像$("form select")这样的东西...... 否则,我建议使用.required-error{border : Apx solid #C80000;}上的css所需的课程,然后您可以使用$(this).addClass('required-error');$(this).removeClass('required-error');

答案 3 :(得分:-1)

$("select").on('blur',function(){ 
  if ($('option:selected', this).val() == 0)
    $(this).css('border-color', '#C80000');     
  else if ($(this).val() > 0)
    $(this).css('border-color', '#BDC7BC');   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
</select>