JQuery Toggle .click函数

时间:2013-05-08 16:07:47

标签: onchange jquery

我正在使用jQuery Mobile。我有一个切换按钮,我需要在用户点击时启动确认对话框。

HTML

<div data-role="fieldcontain">
    <div class='ui-grid-a' >
        <div class='ui-block-a'>
            <h4 rel='tooltip' title='Feature that automatically solves other values as you type in inputs' style='color:#cc0000;'>Auto-Solve</h4>
        </div>
        <div class='ui-block-b'>
            <select name="togSwitch" id="autosolve" data-theme="" data-role="slider" data-mini="true">
                <option value="off">Off</option>
                <option selected value="on">On</option>
            </select>
        </div>
    </div>
</div>

的jQuery

$(document).ready(function() {
$('#autosolve').toggle(function() {
    var answer = confirm('This will erase all current values. Are you sure you want to continue?');
    if (answer === false) {return;} else {reset();}
    }, function() {
    var answer = confirm('This will erase all current values. Are you sure you want to continue?');
    if (answer === false) {return;} else {reset();}
});
});

我也尝试过标准点击功能,但这不起作用:

$('#autosolve').click(function() {
    var answer = confirm('This will erase all current values. Are you sure you want to continue?');
    if (answer === false) {
        return;
    } else {
        reset();
    }
});

我也试过.change()但它启动onload,它还在我的toggleswitch下面显示一个小的选择框,这很奇怪。

有人知道在用户点击切换开关后如何正确启动确认对话框吗?在切换改变之前

1 个答案:

答案 0 :(得分:2)

使用这种方式的toggle()函数在jQuery 1.8中已弃用,并在1.9中删除了!你可能应该为jQ移动切换开关挂钩built in events

$("#autosolve").on('slidestart', function(event) {
    var answer = confirm('This will erase all current values....');
    if ( answer ) reset();
    return answer;    
});