简体   繁体   English

单选按钮未显示为选中状态

[英]Radio Buttons not showing checked

I have created some radio buttons that when clicked show a price. 我创建了一些单选按钮,单击它们可以显示价格。 The code I am using is below.. 我正在使用的代码如下。

   <label for='<?php echo $price; ?>' class="tabs">
   <input type="radio" name="j" class="radioBtn" id='<?php echo $price; ?>' value="<?php echo $price; ?>" onclick="calculate(this);" />
   <?php echo $number; ?>
   </label>

The price is then being shown like this: 价格显示如下:

<span class="prices" id="output"></span>

by 通过

function calculate(obj) { document.getElementById("output").innerHTML = obj.value;}

The prices and buttons are working correctly, but I need to style the label to show that the correct button has being selected. 价格和按钮可以正常工作,但是我需要设置标签的样式,以显示已选择了正确的按钮。 In the DOM it says that the radio input is checked, but it's not being rendered in the HTML. 在DOM中,它表示已检查单选输入,但未在HTML中呈现。

I also need to get the first checkbox to be selected as default. 我还需要将第一个复选框选择为默认复选框。

Can anyone help point me in the right direction at all please? 任何人都可以帮我指出正确的方向吗?

Paddy 稻田

The grofar.com site looks fine to me. grofar.com网站对我来说很好。 When you say the styling isn't working on the label, what exactly do you mean? 当您说样式不适用于标签时,您的意思是什么? Are you trying to apply a different color, font, etc.? 您是否要应用其他颜色,字体等?

As for making the first option selected by default, you have a few options. 至于默认选择第一个选项,则有几个选项。 The first would be to use PHP to determine the first one and add the checked attribute to the input element. 第一种方法是使用PHP确定第一个方法,并将checked属性添加到input元素。 For instance, if you are doing a loop, use an if/then statement to set the checked attribute if the loop counter is 1. 例如,如果您正在执行循环,则如果循环计数器为1,则使用if / then语句来设置checked属性。

A second option is to use jQuery to select the first box: 第二个选项是使用jQuery选择第一个框:

$(function() {
   $("input[name='j']:first").prop("checked", true);
});

However, with that method, your calculate function is not called and therefore the price is not updated. 但是,使用该方法时,不会调用您的calculate函数,因此价格不会更新。

A third option is to use jQuery to simulate a click of the first box: 第三种选择是使用jQuery模拟第一个框的单击:

$(function() {
    $("input[name='j']:first").click();
});

That will select the box and update the price. 这将选择框并更新价格。

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

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