【发布时间】:2019-05-16 15:11:26
【问题描述】:
我创建了一个表单,要求用户填写多个字段(姓名、姓氏、电子邮件、电话号码和 cmets)。
我有函数 validateForm * 带有一个 else if 语句:如果所有字段都正确填写,它将激活一个显示“消息 1”(“你确定要发送你的信息?”)。否则,它将显示一个特定的消息(消息 2、3、4 或 5),该消息根据哪个字段留空而有所不同(即:如果字段“姓名”填写正确,则模式将显示“请输入您的姓氏”;如果两个字段均已填写,则模式将显示“请插入您的电子邮件”;等等...)。
比起对我想要填写的每个字段使用模态,我更喜欢使用模态,将 ID 关联到它并利用 JQuery .empty() 方法来覆盖模态的原始内容的文本每个事件的特定文本(事件 1:姓名字段为空;事件 2:姓氏字段为空,依此类推……)。
我写了以下函数:
function validateForm(){
console.log("test validateForm");
if ($('#name').val()=="") {
$("#testoMyModal").html("Inserisci il nome");
} else if ($('#surname').val()=="") {
$('#testoMyModal').html("Inserisci il cognome");
} else if ($('#email').val()=="") {
$('#testoMyModal').html("Inserisci l'indirizzo eMail");
} else if ($('#numero').val()=="") {
$('#testoMyModal').html("Inserisci il numero di telefono");
} else if ($('#commenti').val()=="") {
$('#testoMyModal').html("Inserisci il testo della richiesta");
} else {
$('#myModal2').modal('show');
}
}
它运行正确是因为控制台不报告错误并且console.log()的值显示正确。但它没有完成这项工作,也就是说它没有执行所需的任务并且没有显示任何消息。
不知道是不是我把与modal相关的ID写错了里面的地方?我的 html 代码如下(我只复制/粘贴与两种模式相关的代码段):
<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div id="testoMyModal" class="modal-body">
<p>Si prega di controllare che tutti i campi siano stati compilati correttamente</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- fine finestra modale di Bootstrap -->
<!-- aggiungo una seconda modale per chiedere conferma della scelta-->
<div class="modal" tabindex="-1" role="dialog" id="myModal2">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Sei sicuro di voler inviare i dati?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Indetro</button>
<button type="button" class="btn btn-primary">Invia</button>
</div>
</div>
</div>
</div>
所以我的问题:
如何使用 jQuery .empty() 方法(通过关联到它的 ID 与它相关)来更改 Modal 中的消息?
- [S O L V E D] - - - - - - - - - >
我通过使用 Bootstrap .modal("show") 方法为用户必须填写的每个文本字段添加指令来解决问题。前面的说明是正确的,但是需要指定显示模态窗口的功能。
正确的sn-p如下:
function validateForm(){
console.log('function "validateForm" has been activated');
if ($('#name').val()=="") {
console.log('validate name');
$("#testoMyModal").html("Inserisci il nome");
$('#myModal').modal('show');
} else if ($('#cognome').val()=="") {
console.log('validate surname');
$('#testoMyModal').html("Inserisci il cognome");
$('#myModal').modal('show');
} else if ($('#email').val()=="") {
console.log('validate email address');
$('#testoMyModal').html("Inserisci l'indirizzo eMail");
$('#myModal').modal('show');
} else if ($('#numero').val()=="") {
console.log('validate number');
$('#testoMyModal').html("Inserisci il numero di telefono");
$('#myModal').modal('show');
} else if ($('#commenti').val()=="") {
console.log('validate text Area');
$('#testoMyModal').html("Inserisci il testo della richiesta");
$('#myModal').modal('show');
} else {
$('#myModal2').modal('show');
console.log('all fields are filled :)');
}
}
现在,只要文本字段为空,模式窗口就会正确显示。
【问题讨论】:
-
您误解了
if/else的工作原理。如果任何if或else if条件为真,则最终的else将永远不会执行。如果 if 或 else if 中的任何一个为真,则需要执行表演。 -
嗨@gibberish,感谢您的评论,我通过指定正确运行的代码编辑了我的问题
标签: jquery twitter-bootstrap bootstrap-modal