Javascript添加选项值

时间:2014-04-10 14:35:08

标签: javascript

我试图添加所选的选项,但它似乎无法正常工作。我希望能够添加所选选项并将其显示在id = tot的文本框中。目前它确实查看了文本框中的值,但它没有添加它只是查看我选择的选项的值。

<script type="text/javascript">

 function check_sm() {
        var value1 = 0;
        var sm1 = document.getElementById("slides");
        for (var i = 0; i < sm1.length; i++) {
            if (sm1[i].selected)
            { value1 = (eval(value1) + eval(sm1[i].value)); }
        } document.getElementById("SCregistration").tot.value = value1;

    }

</script>





<body>
<form>
Do you want a copy of the short course details?
<select id="slides" onChange="check_sm()">
    <option value="50">Yes</option>
    <option value="20">No</option>
</select>

</body>
</form>

1 个答案:

答案 0 :(得分:1)

function check_sm() {
    var slides = document.getElementById("slides");
    var selected = slides.selectedIndex;
    var optValue = parseInt(slides.options[selected].value, 10);
    var oldTot = parseInt(document.getElementById('SCregistration').tot.value, 10);
    document.getElementById('SCregistration').tot.value = oldTot + optValue;
}

DEMO