2 回答
TA贡献1820条经验 获得超9个赞
CSS(添加这些额外的类):
.maincar__currency label{
display: none;
}
.expanded label{
display: block;
}
JS和HTML:
$('.maincar__currency').click(function(e) {
e.preventDefault();
e.stopPropagation();
$(this).toggleClass('expanded');
});
$(".maincar__currency label").click(function(){
$('#' + $(this).attr('for')).prop('checked', true);
var selected = $('input[name=currency-is]:checked').val(); // <-- add this line
$('.active_currency').text(selected.toUpperCase()); // <-- and this line
alert(selected);
});
$(document).click(function() {
$('.maincar__currency').removeClass('expanded');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="maincar__currency">
<div class="active_currency">EUR</div>
<label for="euro-radio-is">
<div class="currency currency__euro">
<img src="/assets/images/icons/euro.png" alt="Euro sign">
<input type="radio" name="currency-is" value="euro" id="euro-radio-is" class="euro_radio_is" checked="true" />
<span class="default">EUR</span>
</div>
</label>
<label for="dollar-radio-is">
<div class="currency currency__dollar">
<img src="/assets/images/icons/dollar.png" alt="Dollar sign">
<input type="radio" name="currency-is" id="dollar-radio-is" class="dollar_radio_is" value="dollar" />
<span>USD</span>
</div>
</label>
<label for="gbp-radio-is">
<div class="currency currency__pound">
<img src="/assets/images/icons/pound-sterling.png" alt="Pound sign">
<input type="radio" name="currency-is" id="gbp-radio-is" class="gbp_radio_is" value="gbp" />
<span>GBP</span>
</div>
</label>
<label for="chf-radio-is">
<div class="currency currency__chf">
<img src="/assets/images/icons/swiss-franc.png" alt="Swiss franc sign">
<input type="radio" name="currency-is" id="chf-radio-is" class="chf_radio_is" value="chf" />
<span>CHF</span>
</div>
</label>
</div>
TA贡献1854条经验 获得超8个赞
尝试使用以下代码:-
$(document).ready(function(){
$("input[type='radio']").click(function(){
var radioValue = $("input[name='currency-is']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
}
});
});
您将获得单选按钮的值。让我知道这是否对您有帮助...
添加回答
举报