- 利用 ASP 和 DotNetNuke 循环遍历重复单选按钮的列表。
- 使用 jQuery
- 当前显示 weightCalculations 函数的正确结果值(例如:3)
- 如何将 rbtCalculations 结果与 weightCalculations 结果结合起来?
例如:如果 rbtCalculations = 非常高并且 weightCalculations = 高 THEN $('p.custom' + ID).text("5");
<input id="rbt_0" name="rbt" value="Very High" checked="checked" onclick="rbtCalculations(this,6559);" type="radio"> <input id="rbt_1" name="rbt" value="High" onclick="rbtCalculations(this,6559);" type="radio"> <input id="stakeholders_rbt_0" name="stakeholders_rbt" value="Very High" onclick="weightCalculations(this,6559);" type="radio"> <input id="stakeholders_rbt_1" name="stakeholders_rbt" value="High" checked="checked" onclick="weightCalculations(this,6559);" type="radio"> <input id="stakeholders_rbt_2" name="stakeholders_rbt" value="Low to Moderate" onclick="weightCalculations(this,6559);" type="radio"> <p class="custom6559">3</p> <script type="text/javascript"> function weightCalculations(value, ID) { if (value.value == "High") { $('p.custom' + ID).text("3"); } else { $('p.custom' + ID).text("2"); } }
问问题
125 次
2 回答
1
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");
}
}
于 2012-04-18T17:11:58.720 回答
1
我可能只是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-18T18:08:52.393 回答