Javascript jQuery-创建单选按钮

Javascript jQuery-创建单选按钮,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想自己创建一个单选按钮。我用jQuery创建样式,用php创建技术。我的问题是风格。我想点击第一个按钮,然后它应该是蓝色的,如果我点击第二个按钮,那么第二个按钮应该是蓝色的,第一个按钮应该是白色的。 我的问题是,背景色永远不会改变。 我知道,变量是可以的(我在控制台中观看)。他们变了!但是背景色不是。 如果有人能帮助我,我将非常感激!!! 谢谢你 HTML: 您需要在中进行样式更改。单击处理程序,如果或同时,则不要使用 $(文档).ready(函数(){ $('#radiobutton_tr

我想自己创建一个单选按钮。我用jQuery创建样式,用php创建技术。我的问题是风格。我想点击第一个按钮,然后它应该是蓝色的,如果我点击第二个按钮,那么第二个按钮应该是蓝色的,第一个按钮应该是白色的。 我的问题是,背景色永远不会改变。 我知道,变量是可以的(我在控制台中观看)。他们变了!但是背景色不是。 如果有人能帮助我,我将非常感激!!! 谢谢你

HTML:


您需要在
中进行样式更改。单击
处理程序,如果
同时
,则不要使用

$(文档).ready(函数(){
$('#radiobutton_true')。单击(函数(){
$('#radiobutton_true').css({
“背景色”:“蓝色”
});
$('#radiobutton_false').css({
“背景色”:“白色”
});
});
$('#radiobutton_false')。单击(函数(){
$('#radiobutton_false').css({
“背景色”:“蓝色”
});
$('#radiobutton_true').css({
“背景色”:“白色”
});
});
});

真的
假的

为什么这里会有两个while循环?你能分享你的小提琴吗?你有一些非常奇怪的流量控制。为什么单击周围的
if
,为什么
while
循环似乎永远不会退出?您有两个while循环(可能无限或从未运行)。我很确定你不想那样做。您需要将它们设置为
if-else-if
,并将其放置在
onchange
的事件处理程序中(或将它们移动到当前的单击事件中)。您的
while
循环是无限循环。Javascript是单线程的,因此事件处理程序不能在循环运行时运行,因此变量永远不会更改。但这对她不起作用
<table class="radiobutton_table" id="radiobutton_table-id">
    <tr class="radiobutton_table-tr" id="radiobutton_table-tr-id">
        <td class="radiobutton_table-tr-td radiobutton_one" id="radiobutton_true">
            <span class="radiobutton-text" id="radiobutton-text-True">True</span>
        </td>
        <td class="radiobutton_table-tr-td radiobutton_two" id="radiobutton_false">
            <span class="radiobutton-text" id="radiobutton-text-False">False</span>
        </td>
    </tr>
</table>
[![var button_1 = false;
var button_2 = false;

$(document).ready(function() {

    if($('#radiobutton_true').click(function() {

        button_1 = true;

        if(button_2 == true) {
            button_2 = false;
        }

    }))

    if($('#radiobutton_false').click(function() {

        button_2 = true;

        if(button_1 == true) {
            button_1 = false;
        }

    }))

    while(button_1 == true) {

        $('#radiobutton_true').css({"background-color": "blue"});
        $('#radiobutton_false').css({"background-color": "white"});

    }

    while(button_2 == true) {

        $('#radiobutton_false').css({"background-color": "blue"});
        $('#radiobutton_true').css({"background-color": "white"});

    }
    })][1]][1]