0

我需要使用从 java 脚本函数返回的值填充隐藏的表单字段,但不确定如何执行此操作。我是 JavaScript 新手。对你的帮助表示感谢。我需要动态填充总价格,如果不是隐藏的表单字段和文本表单字段也可以使用。

太感谢了

<html>
    <head>
        <script type = "text/javascript">
            var allprices = [];
            var index = 0;

            function chkrads(frmName,rbGroupName) {
            var chosen = "";
            var radios = document[frmName].elements[rbGroupName];
            for (var i=0; i <radios.length; i++) {
                if (radios[i].checked || radios[i].selected) {
                    chosen = radios[i].value;
                }


            }

            if (chosen == 0) {printprice = 0}
            if (chosen == 35) {printprice = 35}
            if (chosen == 36) {printprice = 35}
            if (chosen == 29.95) {printprice = 29.95}
            if (chosen == 34.95) {printprice = 34.95}
            if (chosen == 39.45) {printprice = 39.45}
            if (chosen == 44.45) {printprice = 44.45}
            if (chosen == 49.95) {printprice = 49.95}
            if (chosen == 64.45) {printprice = 64.45}
            if (chosen == 74.85) {printprice = 74.85}
            if (chosen == 89.85) {printprice = 89.85}
            if (chosen == 94.40) {printprice = 94.40}
            if (chosen == 99.40) {printprice = 99.40}
            if (chosen == 104.85) {printprice = 104.85}
            if (chosen == 179.70) {printprice = 179.70}
            if (chosen == 209.70) {printprice = 209.70}
            if (chosen == 299.50) {printprice = 299.50}
            if (chosen == 329.45) {printprice = 329.45}
            if (chosen == 349.50) {printprice = 349.50}


            if (rbGroupName == "size") {index = 0}
            if (rbGroupName == "colour") {index = 1}
            if (rbGroupName == "frame") {index = 2}
            if (rbGroupName == "glass") {index = 3}
            if (rbGroupName == "hook") {index = 4}
            if (rbGroupName == "delivery") {index = 5}

            allprices[index] = printprice;

            var totalprice = 0;
            for (var i =0; i <allprices.length; i++) {
                if (!isNaN(allprices[i])) {
                    totalprice = totalprice + allprices[i];
                }
            }

            document.getElementById("result").style.display = "block";
            document.getElementById("result").innerHTML = totalprice;
        }
    </script>
   </head>
   <body>
    <form name= "myform" style="font-size:11px; font-family:Arial, Helvetica, sans-serif;">
        Initial Billing:
         <select name = "size" onchange = "chkrads('myform', 'size')">
            <option></option>
            <option value='29.95'>1 mo 29.95</option>
            <option value='34.95'>1 mo 34.95</option>
            <option value='39.45'>1 mo sh 39.45</option>
            <option value='49.95'>ACT Only 49.95</option>
            <option value='64.45'>ACT+SH 64.45</option>
            <option value='94.40'>Monthly 94.40</option>
            <option value='99.40'>Monthly 99.40</option>
            <option value='74.85'>Qtrly 74.85</option>
            <option value='89.85'>Qtrly 89.85</option>
            <option value='104.85'>Qtrly 104.85</option>
            <option value='179.70'>Semi 179.70</option>
            <option value='209.70'>Semi 209.70</option>
            <option value='299.50'>Annual 299.50</option>
            <option value='329.45'>Annual 329.45</option>
            <option value='349.50'>Annual 349.50</option>
            <option value='49.45'>Mobile Monthly 49.45</option>
            <option value='119.85'>Mobile Qtrly 119.85</option>
            <option value='239.70'>Mobile Semi 239.70</option>
            <option value='439.45'>Mobile Annual 439.45</option>
        </select>

        <input type= "hidden" name = "colour" value = "0">
        Lockbox: <input type = "checkbox" name = "colour" value = "36" onclick = "chkrads('myform', 'colour')">


        <input type= "hidden" name = "frame" value = "0">
        Second button: <input type = "checkbox" name = "frame" value = "35" onclick = "chkrads('myform', 'frame')">



    </form>



    <div id="result" style="display:none;"><strong></strong> <span id="totalprice"></span>     </div>

</body>
</html>
4

2 回答 2

0

您的代码没有说明名称为totalprice的隐藏Input 字段。相反,您有一个span -tag 的 id 为totalprice。如果你想把你的东西放在那里,textContent是你的选择。 https://developer.mozilla.org/en-US/docs/DOM/Node.textContent

此外,如果您使用期权价值的信息而不是复制信息,它会更优雅。

于 2013-04-30T23:07:54.203 回答
0

您可以像填充可见文本框一样填充隐藏的输入字段。也就是说,通过设置它的值。

您需要访问该元素才能更改该值。最简单的方法是为隐藏元素提供一个“id”。

<input id="frameHidden" type= "hidden" name = "frame" value = "0">

然后:

document.getElementById("frameHidden").value = "myVal";
于 2013-04-30T17:52:13.703 回答