如果输入值为1并且选择了input,则jQuery

jQuery if input value is 1 and input is selected

本文关键字:input jQuery 选择 输入 如果      更新时间:2024-04-19

检查输入,如果值为1并且选中,则将span颜色设置为绿色,如果不选中,则设置为红色。我知道我也没有正确选择跨度,我不知道该怎么做。

HTML

<input type="radio"name="rate" value="1" CHECKED>
<span class="radioLabel">Yes</span>
<input type="radio" name="rate" value="0">
<span class="radioLabel">No</span>

jQuery

$('.ratingInput input')
    .each(function() {
        if($(this).val() == ('1') && $(this).is(':selected'))
        {
            $(this + ('span')).css('border','1px solid green');
        } else {
            $(this + ('span')).css('border','1px solid red');
        }
    }

这将完成您想要的

$('input')
    .each(function () {
    if ($(this).val() == ('1') && $(this).is(':checked')) {
        $(this).next('span').css('border', '1px solid green');
    } else {
        $(this).next('span').css('border', '1px solid red');
    }
});

工作演示http://jsfiddle.net/UL3Cn/

有几件事不正确::)

  • 大括号})
  • 而不是选择使用checked:http://api.jquery.com/checked-selector/
  • 对跨度使用.next

其余部分应符合:) 的需要

代码

  $(document).ready(function () {
      $('.ratingInput input').each(function () {
          if ($(this).val() == ('1') && $(this).is(':checked')) {
              $(this).next('span').css('border', '1px solid green');
          } else {
              $(this).next('span').css('border', '1px solid red');
          }
      });
  });

尝试

$(this).next('span').css('border','1px solid green');


小提琴演示

$('input:checked[value="1"]').next('span').css('border', '1px solid green');
$('input[value!="1"]').not(':checked').next('span').css('border', '1px solid red');


fiddle类演示