Bootstrap/JQuery-单选按钮组事件函数

Bootstrap/JQuery - Radio Button Group Event Functions

本文关键字:事件 函数 单选按钮 JQuery- Bootstrap      更新时间:2023-09-26

我的目标是根据选择组中的单选按钮来运行函数。使用其他帖子,我能够实现下面的脚本。以下是我遇到的错误:1) 我声明默认选中的初始按钮不调用函数。如果我从按钮中删除选中的属性,它的功能与其他按钮相同。2) 每个按钮只能工作一次。例如,如果我单击按钮2,它将启动相应的功能。然后,当点击按钮3时,它也能成功工作。问题是当我尝试返回到按钮2时。它不会再开火了。

查询

$("#btn-group-data :input").on("change",function () {
    switch (this.value) {
        case "radar":
            console.log(this.value); 
            //do stuff...
            break;
        case "watches":
            console.log(this.value);
            //do stuff... 
            break;
        case "warnings":
            console.log(this.value);
            //do stuff...
            break;
        case "temp":
            console.log(this.value); // points to the clicked input button
            //do stuff...
            break;
        case "precip":
            console.log(this.value); // points to the clicked input button
            //do stuff...
            break;
        case "snow":
            console.log(this.value); // points to the clicked input button
            //do stuff...
            break;
    }
});

按钮-HTML

<div id="map-container" class="container-fluid">
    <div id="map-div">
        <div id="btn-group-data" data-toggle="buttons">
            <label class="btn btn-primary active">
                <input type="radio" name="radar" value="radar" id="radar" autocomplete="off" checked="" > Radar
            </label>
            <label class="btn btn-primary ">
                <input type="radio" name="watches" value="watches" id="watches" autocomplete="off"> Watches
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="warnings" value="warnings" id="warnings" autocomplete="off"> Warnings
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="temp" value="temp" id="temp" autocomplete="off"> Temperature
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="precip" value="precip" id="precip" autocomplete="off"> Precipitation
            </label>
            <label class="btn btn-primary">
                <input type="radio" name="snow" value="snow" id="snow" autocomplete="off"> Snow
            </label>
        </div>
    </div>
</div>

您应该为所有输入提供相同的名称,如

<div id="map-div">
    <div id="btn-group-data" data-toggle="buttons">
        <label class="btn btn-primary active">
            <input type="radio" name="radar" value="radar" id="radar" autocomplete="off" checked="" > Radar
        </label>
        <label class="btn btn-primary ">
            <input type="radio" name="radar" value="watches" id="watches" autocomplete="off"> Watches
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="radar" value="warnings" id="warnings" autocomplete="off"> Warnings
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="radar" value="temp" id="temp" autocomplete="off"> Temperature
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="radar" value="precip" id="precip" autocomplete="off"> Precipitation
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="radar" value="snow" id="snow" autocomplete="off"> Snow
        </label>
    </div>
</div>