Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择默认选择选项时如何防止JQUERY change()函数触发_Javascript_Jquery_Html - Fatal编程技术网

Javascript 选择默认选择选项时如何防止JQUERY change()函数触发

Javascript 选择默认选择选项时如何防止JQUERY change()函数触发,javascript,jquery,html,Javascript,Jquery,Html,当从下拉菜单中选择默认选择选项时,我想防止JQUERY change()函数触发。但是,如果选择了下拉菜单中的任何其他选项,我希望该函数触发 在下面的示例中,安道尔是默认的选择选项,因此我希望在从下拉列表中选择任何其他国家时触发change()函数 HTML 任何帮助都会很好 将函数包装在if语句中,将默认值(“”)与所选值进行比较 $( ".target" ).change(function() { if($(this).val() != "") { var do

当从下拉菜单中选择默认选择选项时,我想防止JQUERY change()函数触发。但是,如果选择了下拉菜单中的任何其他选项,我希望该函数触发

在下面的示例中,安道尔是默认的选择选项,因此我希望在从下拉列表中选择任何其他国家时触发change()函数

HTML


任何帮助都会很好

将函数包装在if语句中,将默认值(“”)与所选值进行比较

  $( ".target" ).change(function() {
     if($(this).val() != "") {
        var docHeight = $(document).height();
        var scrollTop = $(window).scrollTop(); 
        $('.overlay-bg').show().css({'height' : docHeight}); 
        $('.overlay-content').css({'top': scrollTop+20+'px'});
     }
    });

这里有一个if-needed来帮助澄清(我简化了.change(),但你明白了)。

在DOM就绪时获取默认的选定值,然后将其与更改处理程序中的选定值进行比较-
$(document).ready(function(){
    // show popup when selecting a country from the drop-down
    $( ".target" ).change(function() {
        var docHeight = $(document).height(); //grab the height of the page
        var scrollTop = $(window).scrollTop(); //grab the px value from the top of the page to where you're scrolling
        $('.overlay-bg').show().css({'height' : docHeight}); //display your popup and set height to the page height
        $('.overlay-content').css({'top': scrollTop+20+'px'}); //set the content 20px from the window top
    });
  $( ".target" ).change(function() {
     if($(this).val() != "") {
        var docHeight = $(document).height();
        var scrollTop = $(window).scrollTop(); 
        $('.overlay-bg').show().css({'height' : docHeight}); 
        $('.overlay-content').css({'top': scrollTop+20+'px'});
     }
    });