【发布时间】:2020-06-26 07:52:39
【问题描述】:
我需要在请求后分别确认几个字段。用户需要在新值或旧值之间做出决定。模态正在更新覆盖文本的输入并且不等待确认。它仅在最后一个事件中显示,然后确认答案设置为所有其他事件。我没有在我的研究中找到太多帮助,我不知道如何解决它。
var modalConfirm = function(callback) {
$("#gti-modal-btn-yes").click(function() {
callback(true);
$("#gti-modal").hide();
});
$("#gti-modal-btn-no").click(function() {
callback(false);
$("#gti-modal").hide();
});
$("#gti-modal-btn-close").click(function() {
callback(false);
$("#gti-modal").hide();
});
}
function modalAsk(msg, oldVal, newVal) {
$('.modal').appendTo($('body'));
$("#gti-modal-msg")[0].innerHTML = "<h3>" + msg + "</h3>";
$("#gti-modal").show();
modalConfirm(function(confirm) {
if (confirm) {
oldVal = newVal
alert('Change')
} else {
alert('No Change')
}
});
}
function funConfirmFields(fieldName, oldVal, newVal) {
if (newVal !== "") {
if (newVal !== oldVal) {
if (modalAsk('Warning!</br>Field: " ' + fieldName.toUpperCase() + '"</br>Old value: ' + oldVal + '</br>New values: ' + newVal + '</br></br>Change to New Value?')) {
newVal = oldVal
}
}
} else {
newVal = oldVal;
}
return newVal
}
$(document).ready(function() {
$("#txtName")[0].value = funConfirmFields('Name', $("#txtName")[0].value, "b",);
$("#txtDoc")[0].value = funConfirmFields('Doc', $("#txtDoc")[0].value, "2222");
$("#txtCell")[0].value = funConfirmFields('Cell', $("#txtCell")[0].value, "456");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div id="gti-modal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="gti-modal-Label" aria-hidden="false" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button id="gti-modal-btn-close" type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<p id="gti-modal-titulo" class="modal-title"><i class='fa fa-question-circle pull-rigth '></i> MyModal </p>
</div>
<div id="gti-modal-msg" class="modal-body"></div>
<div class="modal-footer">
<button id="gti-modal-btn-yes" type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">yes</button>
<button id="gti-modal-btn-no" type="button" class="btn btn-primary">No</button>
</div>
</div>
</div>
</div>
<input class="form-control" type="text" id="txtName" name="txtName" value="a"/>
<input class="form-control" type="text" id="txtDoc" name="txtDoc" value= "111"/>
<input class="form-control" type="text" id="txtCell" name="txtCell" value= "123"/>
【问题讨论】:
标签: javascript jquery callback modal-dialog