jQuery根据表单条目计算实时输出

时间:2012-10-03 00:52:46

标签: javascript jquery forms

我有一张表格。用户将填写它,并在该表单的底部我尝试显示插入表单页面的基于值的值。

我到目前为止还没有获得任何输出,这是我的尝试:

HTML表格:

<form id="myform">
 <select   
            name="sv_313"
            onchange="calculate()"
            id="sv_313"
        >
                <option value="Select One">Select One</option>
                <option value="0.1">A</option>
                <option value="0.2">B</option>
                <option value="0.3">C</option>
        </select>
<br/>
           <input 

                type="number" 
                name="sv_312" 
                size="10" 
                maxlength="10"
                onblur="calculate()"
                id="sv_312" 
            />


Calculated value 
            <div id="coverage"> </div>

</form>

javascript:

<script>
//define an array for the possible values input via select menu
var spv = new Array();
spv["A"]=0.1;
spv["B"]=0.2;
spv["C"]=0.3;


//function to get the value from select menu
function getSAMprevalence()
{
var SAMprevalence=0;    
var theForm = document.forms["myform"];
var selectedSAMprevalence = theForm.elements["sv_313"];
SAMprevalence = spv[selectedSAMprevalence.value]
return SAMprevalence;
}

//function to get the value from input field
function getInputvalue()
{
var Inputvalue=0;   
var theForm = document.forms["myform"];
var Inputvalue = theForm.elements["sv_312"];
return Inputvalue;
}

//function to calculate using these two functions, and output into div named coverage:
function calculate()
{
var treatmentCoverage = getSAMprevalence() + getInputvalue();
document.getElementById('coverage').innerHTML =
                                      ""+treatmentCoverage;
}
    </script>

我真的没有javacsript或任何真正的经验,任何指针都会非常感激。谢谢!

编辑:这是一个设置的jsfiddle,说明缺少任何输出:( http://jsfiddle.net/rHu8J/3/

3 个答案:

答案 0 :(得分:2)

尝试使用jQuery ..

DEMO

$(function() {
    $('#sv_313').on('change', function() {
        calculate();
    });

    $('input[name=sv_312]').on('blur', function() {
        calculate();
    });
});

function calculate() {
    var $select = $('#sv_313').val();
    var $text = $('input[name=sv_312]').val();

    if ($select == undefined || $select == '' ||$select == 'Select One') {
        $select = 0;
    }
    else {
        $select = parseFloat($select);
    }
    //
    if ($text == undefined || $text == '') {
        $text = 0;
    }
    else {
        $text = parseFloat($text);
    }

    $('.coverage').html($select + $text);
}​

答案 1 :(得分:1)

HTML:

<form id="myform">
    <select name="sv_313" onchange="calculate()" id="sv_313">
        <option value="0">Select One</option>
        <option value="0.1">A</option>
        <option value="0.2">B</option>
        <option value="0.3">C</option>
    </select>
    <br/>
    <input type="number" id="sv_312" size="10" maxlength="10" onchange="calculate()"/>
</form>
Calculated value 
<div id="coverage"></div>

使用Javascript:

function calculate() {
    var myForm = document.forms['myform'];
    var selectVal = myForm.elements['sv_313'].value;
    var inputVal = myForm.elements['sv_312'].value;
    if(!inputVal) inputVal = 0;
    document.getElementById('coverage').innerHTML = parseFloat(selectVal) + parseInt(inputVal);
}

答案 2 :(得分:0)

我相信您的错误是在您获得所选选项时。试试这个:

SAMprevalence = spv[selectedSAMprevalence.options[selectedSAMprevalence.selectedIndex].text]
谢谢: Get selected value in dropdown list using JavaScript?