我有 2 个相互关联的功能 .._confirm
和_process
. _confirm
负责返回true/false
值_process
并基于响应_process
函数将采取行动并处理投标。
但问题是:
内部_process
正在_confirm
返回undefined
而不是显示布尔值,因为它在数据来自之前继续前进_confirm
注意* 使用 jquery-confirm-3 库进行警报/确认
<script type="text/javascript">
/* Extra confirmation message on place bid */
function _confirm( formname, id_Bid ) {
var response = '';
/* Get bid value, format value and then add to confirm message */
var bidval = jQuery(id_Bid).val();
var bidval = parseFloat(bidval);
if ( bidval > 0 ) {
var floatbidval = bidval.toFixed(2);
var currencyval = "$";
var finalval = currencyval + floatbidval;
if( formname == 'custom' ) {
var confirm1 = 'Do you really want to bid';
} else if( formname == 'direct' ){
var confirm1 = 'Do you really want to directly place this bid';
}
let confirm_message = confirm1 + ' ' + finalval + ' ?';
$.confirm({
title: false,
content: confirm_message,
onAction: function( btnName ) {
response = btnName;
},
buttons: {
confirm: function () {},
cancel: function () {}
}
});
if( response != 'confirm' ) {
event.preventDefault();
return false;
} else{
return true;
}
}
} /* end of function - _confirm() */
function _process( formname, id_Bid ) {
<?php
$enable_bid_place_warning = get_option('uwa_enable_bid_place_warning');
$placebid_ajax_enable = get_option('woo_ua_auctions_placebid_ajax_enable');
/* using page load */
if( $placebid_ajax_enable == 'no' || $placebid_ajax_enable == '' ) {
if( $enable_bid_place_warning == 'yes' ) { ?>
$data = _confirm( formname, id_Bid ); <?php
}
} elseif ( $placebid_ajax_enable == 'yes' ) { /* using ajax */
if( $enable_bid_place_warning == 'yes' ) { ?>
retval = _confirm( formname, id_Bid );
if( retval == true ) {
place_ajax_process( formname );
} <?php
} else { ?>
place_ajax_process( formname );
<?php
}
}
?>
} /* end of function */
</script>
后来在这里研究,我找到了几个使用.Deferred()
和返回承诺的答案..但承诺也在返回unidentified
后来我做了以下更改,但仍然没有运气
function _showConfirmDialog( message ) {
let def = $.Deferred();
$.confirm({
title: false,
content: message,
onAction: function( btnName ) {
def.resolve(btnName);
},
buttons: {
confirm: function () {},
cancel: function () {}
}
});
def.promise();
}
/* Extra confirmation message on place bid */
function _confirm( formname, id_Bid ) {
var response = '';
/* Get bid value, format value and then add to confirm message */
var bidval = jQuery(id_Bid).val();
var bidval = parseFloat(bidval);
if ( bidval > 0 ) {
var floatbidval = bidval.toFixed(2);
var currencyval = "$";
var finalval = currencyval + floatbidval;
if( formname == 'custom' ) {
let confirm1 = 'Do you really want to bid';
} else if( formname == 'direct' ){
let confirm1 = 'Do you really want to directly place this bid';
}
let confirm_message = confirm1 + ' ' + finalval + ' ?';
$.when(_showConfirmDialog(confirm_message)).then(function( response ) {
if ( response == 'confirm' ) {
return true;
} else {
return false;
}
});
}
}
我也尝试使用 async 和 await 但没有运气。