首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Javascript/JQuery在整个表单中添加值

使用Javascript/JQuery在整个表单中添加值
EN

Stack Overflow用户
提问于 2013-08-09 18:25:07
回答 2查看 69关注 0票数 0

在任何其他StackOverflow问题中,我都找不到这个特定的主题:

我正在为医生建一个决策支持页面。

我是JQuery的初学者,我想创建一个变量,当用户通过问卷点击时,这个变量就会加起来。也就是说,有一个包含问题的表,用户单击“是”或“否”,对于每个“是”,就会在总数中添加不同的值。例如,“酗酒史”可能增加4,而“抑郁史”可能增加2。

最后,将和编码为“高风险”(>8)、“中等风险”(5-8)等。

我应该注意,我使用的是JQuery和JQuery手机。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-09 18:40:10

标记

代码语言:javascript
运行
复制
<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

代码语言: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');
    }

});
票数 1
EN

Stack Overflow用户

发布于 2013-08-09 18:41:00

要开始:使用jQuery 1.10.2

代码语言:javascript
运行
复制
// 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 :)
});

这取决于你制定剩下的逻辑,祝你好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18153241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档