Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 页面加载筛选器第二个下拉选项,该选项与下拉列表1的数据值匹配_Javascript_Jquery_Select_Drop Down Menu_Onchange - Fatal编程技术网

Javascript 页面加载筛选器第二个下拉选项,该选项与下拉列表1的数据值匹配

Javascript 页面加载筛选器第二个下拉选项,该选项与下拉列表1的数据值匹配,javascript,jquery,select,drop-down-menu,onchange,Javascript,Jquery,Select,Drop Down Menu,Onchange,我有一段代码,我正在使用它根据第一个下拉列表过滤第二个下拉列表。它与onChange方法配合得很好,它从选择的第一个下拉选项中获取值,然后在第二个下拉选项中查找并匹配选项“data value”以进行筛选 $(document).ready(function() { // get first dropdown and bind change event handler $('#p-city').change(function() { // get optios of second

我有一段代码,我正在使用它根据第一个下拉列表过滤第二个下拉列表。它与onChange方法配合得很好,它从选择的第一个下拉选项中获取值,然后在第二个下拉选项中查找并匹配选项“data value”以进行筛选

$(document).ready(function() {
  // get first dropdown and bind change event handler
  $('#p-city').change(function() {
    // get optios of second dropdown and cache it
    var $options = $('#p-nhb')
    // update the dropdown value if necessary
    .val('')
    // get options
    .find('option')
    // show all of the initially
    .show();
    // check current value is not 0
    if (this.value != '0')
      $options
      // filter out options which is not corresponds to the first option
        .not('[data-val="' + this.value + '"],[data-val=""]')
        // hide them
        .hide();
  });
});
我正在表单上使用它,选项将被保存,因此,如果已经选择了某些内容,如何在页面加载时运行筛选器?

添加
$(“#p-city”).trigger('change')
$('#p-city')之后。更改(function()
,如下代码所示。这应该可以肯定,希望可以。我在JSFIDLE上也尝试过同样的方法,默认情况下只选择第一个选项,并在最后触发更改事件,就可以了

$(document).ready(function() {
    // get first dropdown and bind change event handler
    $('#p-city').change(function() {
    // get optios of second dropdown and cache it
    var $options = $('#p-nhb')
    // update the dropdown value if necessary
    .val('')
    // get options
    .find('option')
    // show all of the initially
    .show();
    // check current value is not 0
    if (this.value != '0')
     $options
    // filter out options which is not corresponds to the first option
    .not('[data-val="' + this.value + '"],[data-val=""]')
    // hide them
    .hide();
    })

  $('#p-city').trigger('change');

});
$('#p-city')之后添加
$('#p-city')。trigger('change');
。更改(function()
,如下代码所示。这应该可以肯定,希望可以。我在JSFIDLE上也尝试过,默认情况下只需选择第一个选项,并在末尾触发更改事件,就可以了

$(document).ready(function() {
    // get first dropdown and bind change event handler
    $('#p-city').change(function() {
    // get optios of second dropdown and cache it
    var $options = $('#p-nhb')
    // update the dropdown value if necessary
    .val('')
    // get options
    .find('option')
    // show all of the initially
    .show();
    // check current value is not 0
    if (this.value != '0')
     $options
    // filter out options which is not corresponds to the first option
    .not('[data-val="' + this.value + '"],[data-val=""]')
    // hide them
    .hide();
    })

  $('#p-city').trigger('change');

});

您可以在
$(文档)中手动触发下拉列表的更改事件。使用
$(“#p-city”)准备好
函数。触发('change');
它什么都没做。您可以将代码放在小提琴或代码片段中,以便我们帮助解决它。您可以在
$(文档)中手动触发下拉列表的更改事件.ready
函数使用
$(“#p-city”).trigger('change');
它什么都没做。你可以把你的代码放到小提琴或代码片段中,这样我们就可以帮助解决它了。