如果输入值为1并且选择了input,则jQuery
jQuery if input value is 1 and input is selected
检查输入,如果值为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类演示
相关文章:
- 我如何在INPUT TEXT中使用这个Javascript和jQuery
- 如何使用keyup或input进行操作?javascript(jQuery)
- 如果输入值为1并且选择了input,则jQuery
- jquery:$(input).keydown无法在单选按钮上设置处理程序,同样的代码也适用于文本
- 快速方法 Javascript/Jquery/etc 检索表单中包含的所有 aspnet:textbox (input)
- jQuery, bootstrap: input.val() 是未定义的,即使 jsfiddle 工作正常
- 如何在jquery中获取td innerhtml值,除了input type=“hidden”
- jQuery提交表单,然后禁用所有“input[type=”submit“]”以防止重复提交
- JavaScript / jQuery input[type=text] pattern
- Jquery .on('input', function(){..});不适用于 IE
- JQuery - 获取input='file'中的所有文件名
- 在input.checked=true/false _without_ jQuery上触发一个事件
- Jquery selector input[type=text]')
- 如何使用“jquery-mentions-input”对多个调用使用多个触发器
- RegEx for jQuery Input Validation
- jQuery 设置 input.value = input.title(或其他输入属性)
- Jquery html attr radio input alt content
- Jquery(input/textarea).val():如何在不更改DOM的情况下添加内容
- JQuery DatePicker Multiple Input
- 如何使用jquery访问表单中的input标签?(包括隐藏类型)