我尝试使用jquery根据某些条件显示一些单选按钮/按钮。然而,它不起作用。任何人都可以让我知道代码中的错误吗?场景是
我的代码是
$('#eForm_registeredDate').change(function() {
$('#divncb').removeClass('dontshow');
var that = $(this);
var currentYear = parseInt(new Date().getFullYear());
var registeredYear = parseInt(that.val());
if(registeredYear == currentYear || currentYear == (registeredYear + 1)) {
$( 'input:radio[name="ncbyear"]').not(':eq(0)').hide();
} else {
var noNcbYear = currentYear - registeredYear;
$( 'input:radio[name="ncbyear"]').not(':gt('+noNcbYear+')').hide();
}
});
这是我的小提琴
答案 0 :(得分:0)
@Twain是对的,你应该用你的raido按钮包裹他们的文本。
当您在每次用户交互时重置对象视图时发生任何更改时,您还需要显示所有无线电。
jQuery的:
$('#eForm_registeredDate').change(function() {
$('#divncb').removeClass('dontshow');
var that = $(this);
var currentYear = parseInt(new Date().getFullYear());
var registeredYear = parseInt(that.val());
$('#divncb label').show();//reset view here
if(registeredYear == currentYear || currentYear == (registeredYear + 1)) {
$( 'input:radio[name="ncbyear"]').not(':eq(0)').parent().hide();
} else {
var noNcbYear = currentYear - registeredYear;
$( 'input:radio[name="ncbyear"]').not(':gt('+noNcbYear+')').parent().hide();
}
});
HTML:
<div id="divncb" class="dontshow">
<label>
<input type="radio" name="ncbyear" value="0" checked>1 year(0%)
</label>
<label>
<input type="radio" name="ncbyear" value="20">2 years(20%)
</label>
<label>
<input type="radio" name="ncbyear" value="25" checked>3 year(25%)
</label>
<label>
<input type="radio" name="ncbyear" value="35">4 years(35%)
</label>
<label>
<input type="radio" name="ncbyear" value="45" checked>5 year(45%)
</label>
<label>
<input type="radio" name="ncbyear" value="50">> 5 years(50%)
</label>
</div>
答案 1 :(得分:0)
在范围中包裹广播字段和标签,并在包装范围内执行显示和隐藏 的 HTML 强>
<div id="divncb" class="dontshow">
<span><input type="radio" name="ncbyear" value="0" checked>1 year(0%)</span>
<span><input type="radio" name="ncbyear" value="20">2 years(20%)</span>
<span><input type="radio" name="ncbyear" value="25" checked>3 year(25%)</span>
<span><input type="radio" name="ncbyear" value="35">4 years(35%)</span>
<span><input type="radio" name="ncbyear" value="45" checked>5 year(45%)</span>
<span><input type="radio" name="ncbyear" value="50">> 5 years(50%)</span>
</div>
JQUERY
$('#eForm_registeredDate').change(function() {
$('#divncb').removeClass('dontshow');
var that = $(this);
var currentYear = parseInt(new Date().getFullYear());
var registeredYear = parseInt(that.val());
$( 'input:radio[name="ncbyear"]').parent().show();
if(registeredYear == currentYear || currentYear == (registeredYear + 1)) {
$( 'input:radio[name="ncbyear"]').not(':eq(0)').parent().hide();
} else {
var noNcbYear = currentYear - registeredYear;
$( 'input:radio[name="ncbyear"]').not(':gt('+noNcbYear+')').parent().hide();
}
});
答案 2 :(得分:0)
不确定您正在尝试做什么
var radios = $('input:radio[name="ncbyear"]');
var registeredDate = $('#eForm_registeredDate');
registeredDate.change(function() {
$('#divncb').removeClass('dontshow');
var that = $(this);
var currentYear = parseInt(new Date().getFullYear(),10);
var registeredYear = parseInt(that.val(),10);
var diff = (currentYear-registeredYear)>0?(currentYear-registeredYear):1;
radios.each(function(index,radio){
if(diff>=index+1)
$(radio).show();
else
$(radio).hide();
})
});