简体   繁体   English

带单选按钮的jquery计算

[英]jquery calculations with radio buttons

Hello Stackoverflow community, 您好Stackoverflow社区,

I am currently trying to implement the following fiddle : http://jsfiddle.net/8D3tJ/2/ into one of my projects, the main difference though is that I am not using select options but input fields / radio buttons. 我目前正在尝试在我的一个项目中实现以下提琴: http : //jsfiddle.net/8D3tJ/2/ ,尽管主要区别在于我不是在使用选择选项,而是在输入字段/单选按钮中使用。

Please see my code: 请查看我的代码:

    <li id="fo143li1" class="notranslate focused">
        <fieldset>
        <div class="price-option">
        <input id="radioDefault_1" name="Field1" type="hidden" value="">
            <span>
            <div class="price" style="float: right; margin-top: 5px;"></div>
        <input id="Field1_0" name="Field1" data-price="5" type="radio" class="field radio" value="5" tabindex="1">
        <label class="choice" for="Field1_0">
            First Choice</label>
            </span>
            <span>
            <div class="price" style="float: right; margin-top: 5px;">300 USD</div>
        <input id="Field1_1" name="Field1" type="radio" class="field radio" value="Second Choice" tabindex="2">
        <label class="choice" for="Field1_1">
            Second Choice</label>
            </span>
            <span>
            <div class="price" style="float: right; margin-top: 5px;">300 USD</div>
        <input id="Field1_2" name="Field1" type="radio" class="field radio" value="Third Choice" tabindex="3">
        <label class="choice" for="Field1_2">
            Third Choice</label>
            </span>
            </div>
        </fieldset>
        </li>
        </ul>
    </form>

    <p class="result" data-base-price="50">&pound;<span>50.00</span></p>

and here my edited jquery part: 这是我编辑的jQuery部分:

    <script type="text/javascript">
    $(document).ready(function () {
        $('.price-option').change(function(){
            var price = parseFloat($('.price').data('base-price'));

            $('.price-option').each(function(i, el) {
                price += parseFloat($('checked:checked', el).data('price'));
            });

            $('.result span').text(price.toFixed(2));
        }); 
    });
    </script>

If I am to try this method based on the markup above I have a NaN value returned. 如果我要根据上面的标记尝试此方法,我将返回一个NaN值。 Any ideas how I can make this working? 有什么想法可以使它正常工作吗? Some expert help would be greatly appreciated. 一些专家的帮助将不胜感激。

<li id="fo143li1" class="notranslate focused">
    <fieldset>
    <div class="price-option">
    <input id="radioDefault_1" name="Field1" type="hidden" value="">
        <span>
        <div class="price" style="float: right; margin-top: 5px;"></div>
    <input id="Field1_0" name="Field1" data-price="5" type="radio" class="field radio" value="5" tabindex="1">
    <label class="choice" for="Field1_0">
        First Choice</label>
        </span>
        <span>
        <div class="price" style="float: right; margin-top: 5px;">300 USD</div>
    <input id="Field1_1" name="Field1" type="radio" data-price="10" class="field radio" value="Second Choice" tabindex="2">
    <label class="choice" for="Field1_1">
        Second Choice</label>
        </span>
        <span>
        <div class="price" style="float: right; margin-top: 5px;">300 USD</div>
    <input id="Field1_2" name="Field1" type="radio" class="field radio" data-price="15" value="Third Choice" tabindex="3">
    <label class="choice" for="Field1_2">
        Third Choice</label>
        </span>
        </div>
    </fieldset>
    </li>
    </ul>
</form>

<p class="result" data-base-price="50">&pound;<span>50.00</span></p>

IN js 在js中

$(document).ready(function () {
    $('.radio').change(function(){
        var price=  parseFloat($('.result').data('base-price'));

        $('.radio').each(function(i, el) {
            if($(this).is(":checked"))
            price += parseFloat($(this).data('price'));
        //    console.log(parseFloat($(this).data('price')));
        });

        $('.result span').text(price.toFixed(2));
    }); 
});

check fiddle here http://jsfiddle.net/C4Lrg/ 在这里检查小提琴http://jsfiddle.net/C4Lrg/

Here is a fiddle using a radio list to update a price 这是一个使用广播列表更新价格的小提琴

http://jsfiddle.net/8D3tJ/23/ http://jsfiddle.net/8D3tJ/23/

HTML: HTML:

<input id="1" name="1" type="radio"  value="5" tabindex="1" class="price-option">
<label for="1">Plus 5</label>

<input id="2" name="1" type="radio"  value="10" tabindex="1" class="price-option">
<label for="2">Plus 10</label>

<p>£<span id="price" base-price="50">50</span></p>

JS: JS:

$(function () {
    $('.price-option').change(function(){
        var price = parseFloat($('#price').attr('base-price'));            

        price += parseFloat($('.price-option:checked').val());

        $('#price').text(price.toFixed(2));
    }); 
});

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

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