简体   繁体   English

如何使用JavaScript显示单选按钮的文本值

[英]How can I display a text value for radio buttons using javascript

I am building an online store where the customer can select custom parts. 我正在建立一个在线商店,客户可以在其中选择定制零件。 I'm quite new to javascript, but I've managed to create a radio button list, where the price is added from each section. 我对javascript还是很陌生,但是我设法创建了一个单选按钮列表,其中从每个部分中添加了价格。

I would like a box to show all of the options selected, not just the sum total. 我希望有一个框显示所有选择的选项,而不仅仅是总和。

I've included the text with value and used parseInt. 我将文本包含在值中并使用了parseInt。 Is there an equivalent I can use to pull the text, not the number, from the value? 我可以用来从值中提取文本而不是数字吗?

My code so far: 到目前为止,我的代码:

<head>
    <script type="text/javascript">
        function DisplayPrice(price){
                        var val1 = 0;
                        for( i = 0; i < document.form1.part.length; i++ ){
                                if( document.form1.part[i].checked == true ){
                                        val1 = document.form1.part[i].value;
                                }
                        }

                        var val2 = 0;
                        for( i = 0; i < document.form2.part2.length; i++ ){
                                if( document.form2.part2[i].checked == true ){
                                        val2 = document.form2.part2[i].value;
                                }
                        }

        var val3 = 0;
                        for( i = 0; i < document.form3.part3.length; i++ ){
                                if( document.form3.part3[i].checked == true ){
                                        val3 = document.form3.part3[i].value;
                                }
                        }


                        var sum=parseInt(val1) + parseInt(val2) + parseInt(val3);
            document.getElementById('totalSum').value=sum;
        }
    </script>
</head>
<body>
    <form name="form1" id="form1" runat="server">
       <br>
       <input id="rdo_1" type="radio" value="0 1.8ghz2xAMD" name="part" checked="checked" onclick="DisplayPrice(this.value);">1.8Ghz Dual Core AMD
        <br>
        <input id="rdo_2" type="radio" value="50 2ghz2xAMD" name="part" onclick="DisplayPrice(this.value);">2Ghz Dual Core AMD
        <br>
    </form>Choose your memory:<br />
    <form name="form2" id="form2" runat="server">
        <br>
        <input id="rdo_1" type="radio" value="0 1333corsair1gb" name="part2" checked="checked" onclick="DisplayPrice(this.value);">1333 Corsair 1GB
        <br>
        <input id="rdo_2" type="radio" value="50 1333corsair2x1gb" name="part2" onclick="DisplayPrice(this.value);">1333 Corsair 2x1GB
        <br>
    </form>Choose your graphics card:<br />
    <form name="form3" id="form3" runat="server">
        <br />
        <input id="rdo_1" type="radio" value="0 5830ATI1gb" name="part3" checked="checked" onclick="DisplayPrice(this.value);">1GB ATI 5830
        <br />
        <input id="rdo_2" type="radio" value="50 5850ATI1gb" name="part3" onclick="DisplayPrice(this.value);">1GB ATI 5850
        <br />
        <input id="rdo_3" type="radio" value="75 5870ATI1gb" name="part3" onclick="DisplayPrice(this.value);">1GB ATI 5870
        <br />
    </form>
</body>

Thanks in advance for any advice you can give. 在此先感谢您提供的任何建议。

The values seem to be consistently separated by a space. 这些值似乎始终由空格分隔。 So you could use split() function to split the value in two parts, the first part containing the price and the second part containing the text. 因此,您可以使用split()函数将值分为两部分,第一部分包含价格,第二部分包含文本。

var parts = value.split(" ");
var price = parseInt(parts[0]);
var text = parts[1];

That said, there are better/nicer ways to achieve the functional requirement, but that's up to you to as an learning exercise. 也就是说,有更好/更巧妙的方法来满足功能要求,但这取决于您的学习。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript中的单选按钮显示值并添加复选框值? - How to display value using radio buttons in javascript and add it with checkboxes value? 如何在 Javascript 中级联单选按钮? - How Can I Cascade Radio Buttons In Javascript? 我尝试使用 class 名称显示单选按钮的值 - I tried to display the value of radio buttons using class name 我们如何使用jquery或javascript中的单选按钮的值显示/隐藏div标签? - how can we show/hide div tags using value of radio buttons in jquery or javascript? 如何使用Javascript / Jquery更改单选按钮的值 - How to change the value of radio buttons using Javascript/Jquery 如何使用javascript检查/取消选中单选按钮 - How do I check/uncheck radio buttons using javascript Vanilla JavaScript - 获取单选按钮的值并在 DOM 中显示该值 - Vanilla JavaScript - Get the value of radio buttons and display the value in the DOM 我正在使用CSS隐藏单选按钮,但是我想使用Javascript将它们显示在一页上 - I am using CSS to hide radio buttons, but I would like to display them on one page using Javascript 如何在JavaScript中为单选按钮分配值 - How to assign radio buttons a value in javascript 如何将单选按钮的值乘以Javascript中的数字,并在文本区域内的文本字符串中显示答案? - How can I multiply a value from a radio button by a number in Javascript and have the answer display in a textstring inside a textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM