javascript - 如何从多个起点触发一个函数?

标签 javascript

我有这个功能:

$(function() {
    $('#toggle-event').change(function() {
        if($(this).prop('checked') == true){
            $('.textfield, .editable_textfield').toggleClass('textfield editable_textfield');
            $('.textfield, .editable_textfield').editable('option', 'disabled', false);

            $('.editable_selectfield_1').editable({
                source: [
                      {value: 'ja', text: 'Ja'},
                      {value: 'nee', text: 'Nee'},
                   ],
                });
            $('.editable_selectfield_1').editable('option', 'disabled', false);
        }
        else
        {
            $('.textfield, .editable_textfield').editable('option', 'disabled', true);
            $('.editable_selectfield_1').editable('option', 'disabled', true);
        }
    });
})

该函数可以通过以下方式触发:

<input id="toggle-event" type="checkbox" data-toggle="toggle" data-size="mini" data-on="<span class='glyphicon glyphicon-pencil'></span>" data-off="<span class='glyphicon glyphicon-pencil'></span>" data-onstyle="danger">

但现在我希望此按钮在同一页面上的多个位置显示。 ID需要是uniqye才能运行脚本,如何实现这一点?

最佳答案

使用类名而不是 ID 作为选择器。在切换项上使用相同的类名。

<input class="toggle" id="toggle-event" type="checkbox".... >

 $('.toggle').change(function() { ....

关于javascript - 如何从多个起点触发一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59106228/

相关文章:

javascript - react native : how can I extend this timer through an alert?

javascript - 如何始终在父元素上捕获 jQuery 事件

javascript - 在以宽度为百分比创建后获取 d3.js SVG 元素的像素宽度

javascript - jquery 或 jquery mobile 中的分页

javascript - Node http.request 什么都不做

javascript - for循环中的SetInterval,传递参数

javascript - 动态发送数据到服务器

javascript - SQL SELECT 和 UPDATE 语法

javascript - 使用 jQuery 可拖动从 Canvas 获取像素颜色?

javascript - 如何根据所做的决定设置指向按钮的链接?