使用<select>标签保存Integer以用于算术</select>

时间:2015-03-31 17:03:32

标签: javascript html select

我只想要一个简单的程序来计算插入select标签中的预定义值并将其乘以文本框的值(也将转换为整数)并将其打印在“prod”文本框中。

<script type="text/javascript">
    //num2 is the id of my select tag
    var index = document.getElementById("num2").selectedIndex;

    //num1 is the id of my textbox
    //prod is the id of product textbox
    function mul(){
        var a = parseInt(document.getElementById("num1").value);
        var b = document.getElementById("num2").value;
        var c = document.getElementById("prod").value;
        c.value=(a*b);
    }

</script>

我疯了......

3 个答案:

答案 0 :(得分:0)

@bloodyKnuckles是正确的,var b需要等于元素,而不是元素的value属性。

working jsfiddle

答案 1 :(得分:0)

您必须获取值并使用parseInt()将其转换为字符串中的整数:

parseInt(document.getElementById("first").value) + parseInt(document.getElementById("second").value);

{{3H2>

答案 2 :(得分:0)

  //num2 is the id of my select tag
    var index = document.getElementById("num2").selectedIndex;

    //num1 is the id of my textbox
    //prod is the id of product textbox
    function mul() {
    debugger
        var a = parseInt(document.getElementById("num1").value);
        var b = document.getElementById("num2").value;
        var c = document.getElementById("prod");
        c.value = (a * b);
    }