简体   繁体   中英

Split radio button values using jquery / Javascript

I want to split the Radio Button values from my form so I know what the option chosen is, as well as the price. The value before the | separator is the price and the value after the | is the option chosen.

<input type="radio" name="arm" value="50|1" id="split" required> Option: 1<br>
<input type="radio" name="arm" value="100|2" id="split" required> Option: 2<br>
<input type="radio" name="arm" value="150|3" id="split" required> Option: 3<br>

<input type="radio" name="mechanism" value="50|1"> Option: 1<br>
<input type="radio" name="mechanism" value="100|2"> Option: 2<br>
<input type="radio" name="mechanism" value="150|3"> Option: 3<br>

<input type="radio" name="base" value="50|1"> Option: 1<br>
<input type="radio" name="base" value="100|2"> Option: 2<br>

<input type="radio" name="fabric" value="50|1"> Option: 1<br>
<input type="radio" name="fabric" value="50|2"> Option: 2<br>
<input type="radio" name="fabric" value="50|3"> Option: 3<br>

<script>

    jQuery(":text[name='quantity'], :radio[name='arm'], :radio[name='mechanism'], :radio[name='base'], :radio[name='fabric']").change(function(){
    var arm = jQuery(":radio[name='arm']:checked");
    var arr = arm.split('|');
    var $mechanism = jQuery(":radio[name='mechanism']:checked");
    var $base = jQuery(":radio[name='base']:checked");
    var $fabric = jQuery(":radio[name='fabric']:checked");
    var $quantity = jQuery(":text[name='quantity']");
    if($arm.length ==1){
        var a = parseInt($arm.val(arr[0]), 10);
        var m = parseInt($mechanism.val(), 10);
        var b = parseInt($base.val(), 10);
        var f = parseInt($fabric.val(), 10);
        var q = parseInt($quantity.val(), 10);
        jQuery("#result").val((a + m + b + f) * q);
    }
    });

</script>

I am trying to split the values using:

var arm = jQuery(":radio[name='arm']:checked");
var arr = arm.split('|');

And passing that value as follows:

 var a = parseInt($arm.val(arr[0]), 10);

This does not work. I am no Jquery or Javascript guru, what should I do to make this work?

This line:

var arr = arm.split('|');

You need to get the value of arm and split that.

var arr = arm.val().split('|');

尝试

var arm = jQuery(":radio[name='arm']:checked").val();

var arr = arm.split('|');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM