停止执行JQuery对话框中的代码,如JQuery Confirm



Html选择标签

<select name="testSelect" id="testSelect" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

Jquery代码:-

(function () {
var previous;
$("#testSelect").on('focus', function () {                
previous = $(this).val();
}).change(function() {                
var r = confirm("If you change display type, default value fields will get reset !");
if (r === true) {
previous = $(this).val();                                      
} 
else {
console.log(previous);
$(this).val(previous);                 
}                       
});
})();

如果我按"否",我将使用此代码恢复以前选择的值。当我使用Jquery confirm时,它可以正常工作。这里Jquery confirm在我更改任何值时停止执行,除非我按Yes或no。

但现在我想删除"确认"框,并希望使用Jquery对话框进行确认。像这样:-

$( function() {
var previous;
$("#testSelect").on('focus', function () { 
previous = $(this).val();
}).change(function(event) {   
var that = this;                                               
$( "#dialog-confirm" ).dialog({                  
resizable: false, height: "auto", width: 400, async: false, modal: true,
buttons: {
Yes: function() { 
console.log($(that).val());                     
previous = $(that).val();
$( this ).dialog( "close" );                  
},
No: function() {                      
$(that).val(previous);
$( this ).dialog( "close" );
}
}
});                
})

}(;

但问题是。。即使有对话框确认弹出窗口,代码也会被执行。它不会像Jquery确认框那样停止。我试了很多东西,但都不起作用,有人能帮忙吗?

请记住:JQuery确认中一切正常。。。。但无法在Jquery对话框中工作。。。

您需要您的代码处于jQuery文档就绪状态,这样它只有在页面完成加载时才会被调用,并将您的事件绑定到#testSelect。

$( document ).ready(function() {
var previous;
$("#testSelect").on('focus', function () { 
previous = $(this).val();
}).change(function(event) {   
var that = this;                                               
$( "#dialog-confirm" ).dialog({                  
resizable: false, height: "auto", width: 400, async: false, modal: true,
buttons: {
Yes: function() { 
console.log($(that).val());                     
previous = $(that).val();
$( this ).dialog( "close" );                  
},
No: function() {                      
$(that).val(previous);
$( this ).dialog( "close" );
}
}
});                
})

});

最新更新