简体   繁体   English

JS Selectbox函数出现问题

[英]Problem with JS Selectbox Function

I have a selectbox with three options. 我有一个带有三个选项的选择框。 When a user selects one of the three options, I want a specific div to appear below it. 当用户选择三个选项之一时,我希望在其下方显示一个特定的div。 I am trying to write the code that dictates which specific box is to appear when each of the three options is selected. 我正在尝试编写指示在选择三个选项中的每一个时显示哪个特定框的代码。 So far, I have only worked on the code that pertains to the first option. 到目前为止,我只从事与第一种选择有关的代码。 However, whenever the user selects any of the three options from the selectbox, the function for the first option is triggered and the div is displayed. 但是,只要用户从选择框中选择三个选项中的任何一个,就会触发第一个选项的功能并显示div。

My question is two part: 1) How do I write a conditional function that specifically targets the selected option 我的问题分为两部分:1)我如何编写专门针对所选选项的条件函数

2) What is the best way to accomplish what I have described above; 2)完成上述工作的最佳方法是什么? How do I efficiently go about defining three different functions for three different options in a select box? 如何在选择框中为三个不同的选项有效地定义三个不同的功能?

Here is the function I was working on for the first option: 这是我为第一个选项使用的功能:

$(document).ready(function(){
var subTableDiv = $("div.subTableDiv");
var subTableDiv1 = $("div.subTableDiv1");
var subTableDiv2 = $("div.subTableDiv2");
subTableDiv.hide();
subTableDiv1.hide();
subTableDiv2.hide();

var selectmenu=document.getElementById("customfields-s-18-s");
selectmenu.onchange=function(){ //run some code when "onchange" event fires
 var chosenoption=this.options[this.selectedIndex].value //this refers to "selectmenu"
 if (chosenoption.value ="Co-Op"){
     subTableDiv1.slideDown("medium");
     }
 }
});

Html: HTML:

<tr>
<div>
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" >
<option value="Condominium"> Condominium</option>
<option value="Co-Op"> Co-Op</option>
<option value="Condop"> Condop</option>
</select>
</div>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Condominium" class="subTableDiv">Hi There! This is the first Box</div>
</td>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Co-Op" class="subTableDiv1">Hi There! This is the Second Box</div>
</td>
</tr>
<tr class="subTable">
<td colspan="2">
<div style="background-color: #EEEEEE; border: 1px solid #CCCCCC; padding: 10px;" id="Condop" class="subTableDiv2">Hi There! This is the Third Box.</div>
</td>
</tr>

I think you can get this using the position of the item in the list and the table, as long as those relative positions are the same. 我认为,只要这些相对位置相同,就可以使用列表和表格中项目的位置来获取此信息。 Change the class on the DIVs so they are all subTableDiv . 更改DIV上的类,使它们全部都是subTableDiv

$(function() {
    $('#customfields-s-18-s').change( function() {
         var selected = $(this).find('option:selected');
         var position = $(this).find('option').index(selected);
         // hide all then show the nth one
         $('.subTableDiv').hide().eq(position).show();
    });
});

You can use selectmenu.value (or $(selectmenu).val() ) to get the value of the selected option, and you can match the functions to the values using an object. 您可以使用selectmenu.value (或$(selectmenu).val() )来获取所选选项的值,并且可以使用对象将函数与值进行匹配。 Example: 例:

$(function() {
  var call_table = {
    'Condominium': function() {alert('One!');},
    'Co-Op': function() {alert('Two!');},
    'Condop': function() {alert('Three!');}
  };

  $('#customfields-s-18-s').change(function() {
    call_table[this.value]();
  });
});

Of course, you don't have to define the functions inline. 当然,您不必内联定义函数。 I just did it for concision here. 我只是为了简洁起见。 You could define them anywhere and reference them by name instead. 您可以在任何地方定义它们,并通过名称来引用它们。

It looks like the select option values are the same as the IDs for the divs. 看起来选择选项的值与div的ID相同。 You could use that to define a function that basically shows the div that has the same id as the value of the selected option. 您可以使用它来定义一个函数,该函数基本上显示与所选选项的值具有相同ID的div。 Also change the class on each div to subtableDiv . 还要将每个div上的类更改为subtableDiv

$("#customfields-s-18-s").change(function() {
    // hide all divs
    $('.subtableDiv').hide();
    // show matching div
    var value = $(this).val();
    $('#' + value).show();
}

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

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