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