简体   繁体   English

确认前确认消息

[英]verify before confirmation message

I'm trying to check if the '#text' (id for a textarea) is empty when I change '#my_selection' (id for a drop-down select) option. 我尝试更改“ #my_selection”(下拉选择的ID)选项时,是否检查“ #text”(文本区域的ID)是否为空。 And if NOT EMPTY (ie, there's some text in the textarea), I would like the confirmation to pop up, else don't want to change the '#my_selection'. 如果不是空的(例如,文本区域中有一些文本),我想弹出确认,否则不想更改“ #my_selection”。
Many thanks in advance. 提前谢谢了。

var selected=$('#my_selection').val();
$('#my_selection').change(function(){
   if($("#text").val() != ""){
      var check=confirm("change?");
      if(check){
         selected=$(this).val();
         $('#my_selection').val(selected);
      }else{
         $(this).val(selected);
      }
   }
});

As far as I can tell you code is ok. 据我所知,代码还可以。 Perhaps you are just missing an else on the outter if: 如果满足以下条件,也许您只是在外面错过了另一个:

var oldVal = $('#select').val();
$('#select').change(function(){
    if ($('#text').val() != ''){
        if(confirm('change ?')){
            oldVal=this.value;
        } else {
            this.value = oldVal;
        }
    } else {
        this.value = oldVal;
    }
});

You can test a running example here . 您可以在此处测试运行示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM