JavaScript 并获取单选按钮的值

标签 javascript html output

我尝试使用 javascript 创建在线计算表单,除了单选按钮之外一切正常。

场景:

x(select list) =
   item1 - value="11.2"
   item2 - value="7.6"
   item3 - value="7"

y=(input number)

z=(input number)

coverkind=(radio button)
   if 1 selected >> coverkind = z*800
   if 2 selected >> coverkind = ((y/16)+1)*8*z

totalprice= (x*y*z)+(1000*z)+coverkind

到目前为止我的工作:

<html>
<head>
    <script type="text/javascript">
     function getselectionPrice() {
     var elt = document.getElementById("selectionone");
     var selectionPrice = elt.options[elt.selectedIndex].value;
     var y = document.getElementById("y").value;
     var z = document.getElementById("z").value;
     var ser = (selectionPrice * y * z);
     var dz = z*1000;

     var coverkind = document.getElementById("cover").value;
     if (coverkind == 'soft') {
         var SizePrice = (z * 800);
     } else {
         var SizePrice = ((y / 16) + 1) * 8 * z;
     }

     var finalPrice =  ser + dz;
     document.getElementById("totalPrice").value = finalPrice;
 }
    </script>
</head>
<body>
    <div>

        <form action="" id="calcform" onsubmit="return false;">
            <div>
                <div>
                    <fieldset>
                        <label>select</label>
                        <select id="selectionone" name='selectionone' onChange="getselectionPrice()">
                            <option value="11.2">1</option>
                            <option value="7.6">2</option>
                            <option value="7">3</option>
                        </select>
                        <br/>
                        <p>y
                            <input type="text" id="y" onchange="getselectionPrice()" />
                        </p>
                        <p>z
                            <input type="text" id="z" onchange="getselectionPrice()" />
                        </p>
                        <label>cover</label>
                        <input type="radio" name="cover" value="hard" />hard
                        <br />
                        <br>
                        <input type="radio" name="cover" value="soft" />soft
                        <br>
                        <br>
                        <br/>The new calculated price:
                        <INPUT type="text" id="totalPrice" Size=8>
                    </fieldset>
                </div>
                <input type='submit' id='submit' value='Submit' onclick="getselectionPrice()" />
            </div>
        </form>
    </div>
    </body>
    </html>

如果我从 js 中删除 coverkind,其余的工作正常。我在谷歌上搜索了有关从单选按钮获取值(value)的信息,但没有发现与这种情况非常相关的内容。 firebug 错误面板:

类型错误:document.getElementById(...) 为空 var coverkind = document.getElementById("cover").value;

最佳答案

如果你会使用jQuery,你可以在一行中获取选中的单选按钮的值。

var Val = $("input[name=cover]:checked").val();

关于JavaScript 并获取单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420248/

相关文章:

Javascript Highcharts v3.0.5 - 使用多个 Y 轴时如何隐藏 Y 轴标题

javascript - 通过javascript在表中动态添加 "select"和 "default Text"

javascript - 单击黑暗区域时如何关闭侧面菜单?

valgrind - Callgrind 输出不包括函数调用

可以增加程序输出的文本限制吗?

javascript - 原型(prototype)框架破坏其他脚本

javascript - 在 jQuery 中使用克隆方法时摆脱文本框的 Value 属性

javascript - Paypal 加iframe

html - 图片旁边的段落

java - 仅打印循环中的最后一个数字