发布于 2012-04-18 18:08:52
我可能只是在单选按钮中添加一个class来识别它们,并添加一个包装元素来将所有部件关联在一起:
<div class="js-weight-calc-wrap">
<input type="radio" class="js-rbt" ... />
<input type="radio" class="js-rbt" ... />
<input type="radio" class="js-weight" ... />
<input type="radio" class="js-weight" ... />
<input type="radio" class="js-weight" ... />
<p class="js-result custom6559"></p>
</div>
<script>
jQuery(document).ready(function ($) {
$('.js-rbt, .js-weight').change(function () {
var $this = $(this),
$wrap = $this.closest('.js-weight-calc-wrap'),
rbtVal = $wrap.find('.js-rbt:checked').val(),
weightVal = $wrap.find('.js-weight:checked').val(),
result = rbtVal === 'VeryHigh' && weightVal === 'High'
? '5'
: rbtVal === 'VeryHigh' && weightVal === 'Low'
? '4'
: '0';
$wrap.find('.js-result').text(result)
});
});
</script>我可能还会创建一个包含所有逻辑的jQuery插件,所以在您的页面上,它只是一个调用,如下所示:
jQuery(function ($) {
$('.js-weight-calc-wrap').weightCalculator({
rbtSelector: '.js-rbt',
weightSelector: '.js-weight',
resultSelector: '.js-result'
});
});更新
我以前已经忘记了,您在选择单选按钮时需要过滤它们,这样您就只能得到选中的按钮(与<select/>元素混淆)。将:select添加到选择器后,它将按预期工作。我稍微清理了一下,并创建了一个工作jsFiddle。
发布于 2012-04-18 17:11:58
function weightCalculations(value, ID) {
if (value.value === "High" && $('input[name="rbt"]').val() === "Very High") {
$('p.custom' + ID).text("5");
}
else if(value.value === "High"){
$('p.custom' + ID).text("3");
}
else {
$('p.custom' + ID).text("2");
}
}https://stackoverflow.com/questions/10213299
复制相似问题