1

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

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

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/

4

3 回答 3

3

尝试使用 jQuery ..

演示

$(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);
}​
于 2012-10-03T01:15:08.537 回答
2

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);
}
于 2012-10-03T01:35:39.420 回答
0

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

SAMprevalence = spv[selectedSAMprevalence.options[selectedSAMprevalence.selectedIndex].text]

感谢: 使用 JavaScript 在下拉列表中获取选定的值?

于 2012-10-03T00:57:27.737 回答