开发者

jQuery disable an element be used to turn off a function?

开发者 https://www.devze.com 2023-01-14 13:06 出处:网络
Can the same \"disable\" be used to开发者_Go百科 turn off a function using a checkbox for example?Any out there?If I\'ve understood what you\'re asking for, you could assign and remove a click handler

Can the same "disable" be used to开发者_Go百科 turn off a function using a checkbox for example? Any out there?


If I've understood what you're asking for, you could assign and remove a click handler for a function, or something along the lines:

<script type="text/javascript">

    function toggleStatus() {
        if ($('#toggle').is(':checked')) {
            $('#alert').click(function() {
                hello();
            });

            $("#toggleLabel").text("on");

        } else {
            $("#toggleLabel").text("off");
            $('#alert').unbind('click');
        }   
    }

    function hello() {
        alert('hello');
    }

</script>

<form id="toggleForm">
    <label id="toggleLabel" for="toggle">off</label>
    <input type="checkbox" id="toggle" name="toggle" onchange="toggleStatus()" />
</form>

<a id="alert" href="#">Function</a>

--

Update:

Hmm, not sure if this is ideal, but this should mimic what you're looking for:

<script type="text/javascript">

    function toggleStatus() {
        if ($('#toggle').is(':checked')) {
            $("#toggleLabel").text("on");
            $("#alert").tipTip();
            $("#tiptip_holder").css('display', '');
        } else {
            $("#tiptip_holder").css('display', 'none !important');
            $("#toggleLabel").text("off");
        }   
    }

</script>

<form id="toggleForm">
    <label id="toggleLabel" for="toggle">off</label>
    <input type="checkbox" id="toggle" name="toggle" onchange="toggleStatus()" />
</form>

<a id="alert" href="#" title="This is a tooltip">Tooltip</a>

I tried looking into unbinding etc, but nothing seemed to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消