简体   繁体   English

jQuery不显示选择元素。 控制台中没有错误

[英]Jquery not showing select element. No errors in console

I have a "state" select and a few "carrier" selects. 我有一个“状态”选择和一些“载体”选择。 for simplicity I'm using only two carrier selects here. 为简单起见,我在这里仅使用两个运营商选择。 My jquery is suppose to show the carrier selects based on the the state selected. 我的jquery假设显示基于所选状态的运营商选择。 The state select value is appended to the carrier select name so I can choose which carrier select to add a specific class to. 状态选择值附加到载体选择名称,因此我可以选择要向其添加特定类的载体选择。

MY PROBLEM: My carrier selects wont show up. 我的问题:我的承运人选择不会显示。 I had this working at one point, and I must've changed something along the way. 我曾经在某一点上完成过这项工作,而且在此过程中我必须做出了一些改变。 Not sure whats happening here. 不知道这里发生了什么。 Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

EDIT: I've added my original JS to show where I was, and how I want to change to Jquery. 编辑:我已经添加了原始的JS,以显示我在哪里,以及我想如何更改为Jquery。

HTML: HTML:

<div style="width:160px;">
  <select name="state_select" id="state_select">
    <option value="0" selected>Choose a state</option>
    <option value="1">Connecticut</option>
    <option value="2">New Hampshire</option>
    <option value="3">New Jersey</option>
    <option value="4">New York</option>
  </select>
</div>
<div id="select-div1" class="select-div">
  <select name="carrier_select" id="carrier_select1" class="carrier_select">
    <option selected disabled>Select a carrier - Conn</option>
    <!--PHP GENERATED OPTIONS-->
  </select>
</div>
<div id="select-div" class="select-div">                      
    <select name="carrier_select" id="carrier_select2" class="carrier_select">
        <option selected disabled>Select a carrier - NH</option>
            <!--PHP GENERATED OPTIONS-->
    </select>
</div>

JQUERY: JQUERY:

$('#state_select').prop('selectedIndex', 0);
$('.carrier_select').prop('selectedIndex', 0);

function optionCheck() {
  stateVal = $('div.select-div select').val();
  selectDiv = $('#carrier_select')[0] + stateVal;

  if ($(stateVal).attr('selected', 'selected')) {
    $(selectDiv).attr('class', "conn_select", "nh_select", "nj_select", "ny_select");
    $(selectDiv).addClass("dropdown-box");
  } else {
    $(selectDiv).attr('class', 'carrier_select');
  }
}
$('#state_select').change(function(e) {
  $('.carrier_select').prop('selectedIndex', 0);
  optionCheck();
});

MY JAVASCRIPT (WORKS) BEFORE TRYING JQUERY: 尝试JQUERY之前,我的Java脚本(工作):

function optionCheck() { 
   var i, len, optionVal, selectDiv,
   selectOptions = document.getElementById("state_select");

   // loop through the options in case there
   // are multiple selected values
   for (i = 0, len = selectOptions.options.length; i < len; i++) {
       // get the selected option value
       optionVal = selectOptions.options[i].value;
       // find the corresponding help div
       selectDiv = document.getElementById("carrier_select" + optionVal);
       // move on if I didn't find one
       if (!selectDiv) { continue; }
       // set CSS classes to show/hide help div
       if (selectOptions.options[i].selected) {
          selectDiv.className = "conn_select nh_select nj_select ny_select";
          $(selectDiv).addClass("dropdown-box");
       } else {
          //Hide carrier select on page load
          selectDiv.className = "carrier_select";
       }
   }   
 }
// bind the onchange handler
document.getElementById("state_select").onchange = optionCheck;

CSS: CSS:

.select-div select {
  border: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  text-indent: 1px;
  text-overflow: '';
}

.carrier_select {
  display: none;
}

You are telling css to hide the element 您正在告诉CSS隐藏元素

.carrier_select {
  display: none;
}

Both of your select elements have the class "carrier_select" and as a result of this css definition, they are not displayed. 您的两个select元素都具有“ carrier_select”类,并且由于此CSS定义,它们不会显示。 Remove or change this definition for them to be shown. 删除或更改此定义以使其显示。

Edited in such a way that you can see the carrier selections. 以可以看到运营商选择的方式进行编辑。 But other part of your question - appending to carrier name, showing carriers depending on the state needs more inputs. 但是问题的其他部分-附加到承运人名称之后,根据州显示承运人需要更多输入。

 $('#state_select').prop('selectedIndex', 0); $('.carrier_select').prop('selectedIndex', 0); function optionCheck() { stateVal = $('div.select-div select').val(); selectDiv = $('#carrier_select')[0] + stateVal; if ($(stateVal).attr('selected', 'selected')) { $(selectDiv).attr('class', "conn_select", "nh_select", "nj_select", "ny_select"); $(selectDiv).addClass("dropdown-box"); } else { $(selectDiv).attr('class', 'carrier_select'); } } $('#state_select').change(function(e) { $('.carrier_select').prop('selectedIndex', 0); optionCheck(); }); 
 .select-div select { border: none; -webkit-appearance: none; -moz-appearance: none; text-indent: 1px; text-overflow: ''; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="dropdown-box" style="width:160px;"> <select name="state_select" id="state_select"> <option value="0" selected>Choose a state</option> <option value="1">Connecticut</option> <option value="2">New Hampshire</option> <option value="3">New Jersey</option> <option value="4">New York</option> </select> </div> <div id="select-div1" class="select-div"> <select name="carrier_select" id="carrier_select1" class="carrier_select"> <option selected disabled>Select a carrier - Conn</option> <!--PHP GENERATED OPTIONS--> </select> </div> <div id="select-div" class="select-div"> <select name="carrier_select" id="carrier_select2" class="carrier_select"> <option selected disabled>Select a carrier - NH</option> <!--PHP GENERATED OPTIONS--> </select> </div> 

First off, your code has some redundancies and some questionable decisions in it in my humble opinion that you could work around in order to simplify and/or make it more usable. 首先,根据我的拙见,您的代码中有一些冗余和一些可疑的决定,您可以解决这些问题以简化和/或使其更易使用。 However, there is a way to achieve what you want with most of it untouched, using Javascript/jQuery code. 但是,有一种方法可以使用Javascript/jQuery代码在大多数条件不变的情况下实现您想要的功能。 For the full thing, check this fiddle , the script is below as well along with its explanation: 有关完整的信息,请检查此小提琴 ,该脚本及其说明也在下面:

$('#state_select').prop('selectedIndex', 0);
$('.carrier_select').prop('selectedIndex', 0);

function optionCheck() {
  stateVal = $('#state_select').val();
  selectDiv = $('#carrier_select'+ stateVal);
    $('.dropdown-box:not(#state_select_box)').removeClass('dropdown-box').addClass('carrier_select');
    $(selectDiv).removeClass('carrier_select').addClass('dropdown-box');    
    $($selectDiv).val('0');
}
$('#state_select').change(function(e) {
  optionCheck();
});

What this does is it gets the val() of #state_select , appends it to the #carrier_select so that the selector targets the right id, then changes all active selectors, except the #state_selector_box (which I made to wrap around #state_select ) to ones with the .carrier_select class, thus making them invisible and then it finally makes the one that corresponds to the selected state visible using the dropdown-box class. 它的作用是获取#state_selectval() ,将其附加到#carrier_select以便选择器定位正确的ID,然后将除#state_selector_box (我将其包裹在#state_select周围)之外的所有活动选择器更改为带有.carrier_select类的对象,从而使其不可见,然后最终通过dropdown-box类使与所选状态相对应的对象可见。 Also the val() of the selector that just appeared is set to 0 . 刚出现的选择器的val()也设置为0

Without commenting on the rest of the code, try removing 在不评论其余代码的情况下,尝试删除

.carrier_select {
    display: none;
}

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

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