简体   繁体   中英

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. 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'.
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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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