Hello Stackoverflow社区,
我目前正在尝试在我的一个项目中实现以下小提琴:http://jsfiddle.net/8D3tJ/2/,但主要区别在于我没有使用选择选项而是输入字段/单选按钮。
请参阅我的代码:
<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">£<span>50.00</span></p>
这里是我编辑的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>
如果我要根据上面的标记尝试这种方法,我会返回一个NaN值。 我有什么想法可以使这个工作吗?非常感谢一些专家的帮助。
答案 0 :(得分:3)
<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">£<span>50.00</span></p>
IN 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));
});
});
检查小提琴 这里http://jsfiddle.net/C4Lrg/
答案 1 :(得分:1)
这是一个使用无线电列表来更新价格的小提琴
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:
$(function () {
$('.price-option').change(function(){
var price = parseFloat($('#price').attr('base-price'));
price += parseFloat($('.price-option:checked').val());
$('#price').text(price.toFixed(2));
});
});