在任何其他StackOverflow问题中,我都找不到这个特定的主题:
我正在为医生建一个决策支持页面。
我是JQuery的初学者,我想创建一个变量,当用户通过问卷点击时,这个变量就会加起来。也就是说,有一个包含问题的表,用户单击“是”或“否”,对于每个“是”,就会在总数中添加不同的值。例如,“酗酒史”可能增加4,而“抑郁史”可能增加2。
最后,将和编码为“高风险”(>8)、“中等风险”(5-8)等。
我应该注意,我使用的是JQuery和JQuery手机。
发布于 2013-08-09 18:40:10
标记
<input class=questionaire id="question1" type=checkbox value=1>Question 1<br>
<input class=questionaire id="question2" type=checkbox value=2>Question 2<br>
<input class=questionaire id="question3" type=checkbox value=3>Question 3<br>
<input class=questionaire id="question4" type=checkbox value=2>Question 4<br>
<input class=questionaire id="question5" type=checkbox value=1>Question 5<br>
<input class=questionaire id="question6" type=checkbox value=1>Question 6<br>
<input class=questionaire id="question7" type=checkbox value=3>Question 7<br>
<input class=questionaire id="question8" type=checkbox value=2>Question 8<br>
<input class=questionaire id="question9" type=checkbox value=5>Question 9<br>
<div id=message>
The color of this changes as the score gets higher, 0 is black, <8 is blue, >8 is red.
</div>
JavaScript
$('.questionaire').change(function () {
//sum it up
var sum = 0;
$('.questionaire:checked').each(function (index) {
sum = sum + parseFloat($(this).val());
});
if (sum == 0) {
$('#message').attr('style', 'color:black');
}
if (sum < 8 and sum > 0) {
$('#message').attr('style', 'color:blue');
}
if (sum > 8) {
$('#message').attr('style', 'color:red');
}
});
发布于 2013-08-09 18:41:00
要开始:使用jQuery 1.10.2
// Attach a change event listener to all radio buttons on the page
// This means that when you click a radio button then it will go into the function(){} code
$(document).on('change', 'input[type="radio"]', function(){ // this nifty code says that "anytime a radio button is clicked in the scope of the document then fire off this function" If you plan on dynamically adding radio buttons then make sure to use this format
//woot, we're in!
//spit out the value of the changed radio button
console.log($(this).val()); //or this.value
// console.log(); is gonna be your very best friend :)
});
这取决于你制定剩下的逻辑,祝你好运!
https://stackoverflow.com/questions/18153241
复制相似问题