0

我正在使用 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,并且它还在我的切换开关下方显示一个小选择框,这很奇怪。

一旦用户单击切换开关,有谁知道如何正确启动确认对话框?在切换开关改变之前

4

1 回答 1

2

以这种方式使用的toggle()函数在 jQuery 1.8 中已被弃用,并在 1.9 中被删除!您可能应该挂钩到jQ 移动切换开关的内置事件:

$("#autosolve").on('slidestart', function(event) {
    var answer = confirm('This will erase all current values....');
    if ( answer ) reset();
    return answer;    
});
于 2013-05-08T16:14:44.747 回答