jQuery根据选项选择显示/隐藏Div

时间:2014-11-24 10:27:01

标签: javascript jquery html css

我正在尝试根据选择框选择项目

更改div

这是我的JSFiddle

这里我有以下代码

<select id="test" name="form_select">
   <option value="0" selected>No</option>
   <option value ="1">One</option>
   <option value ="2">Two</option>
   <option value ="3">Three</option>
</select>

<div id="first" style="display: none;">First Div</div>
<div id="second" style="display: none;">Second Div</div>
<div id="third" style="display: none;">Third Div</div>
<div id="none" style="display: none;">Nothing to Displayt</div>

和JS To

document.getElementById('test').addEventListener('change', function ()
{
var style = this.value == 1 ? 'block' : 'none';
document.getElementById('none').style.display = style;
}
};

我需要的是根据选择显示各自。写的事件没有触发......我正在做的错误是什么,我该如何解决这个问题?

6 个答案:

答案 0 :(得分:4)

您的代码中存在语法错误......然后

&#13;
&#13;
var testel = document.getElementById('test');
testel.addEventListener('change', function() {
  testChange(this.value)
});
//initialize
testChange(testel.value)

function testChange(value) {
  document.getElementById('none').style.display = value == 0 ? 'block' : 'none';;
  document.getElementById('first').style.display = value == 1 ? 'block' : 'none';;
  document.getElementById('second').style.display = value == 2 ? 'block' : 'none';;
  document.getElementById('third').style.display = value == 3 ? 'block' : 'none';;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="test" name="form_select">
  <option value="0" selected>No</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<div id="first" style="display: none;">First Div</div>
<div id="second" style="display: none;">Second Div</div>
<div id="third" style="display: none;">Third Div</div>
<div id="none" style="display: none;">Nothing to Displayt</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

js代码中存在轻微的语法错误。请找到updated fiddle

JS代码 -

  

document.getElementById('test')。addEventListener('change',function   ()       {
      var style = this.value == 1? '阻止':'无';
      document.getElementById('none')。style.display = style;
  style = this.value == 1? 'block':'none';

document.getElementById('first').style.display = style;

style = this.value == 2 ? 'block' : 'none';

document.getElementById('second').style.display = style;

style = this.value == 3 ? 'block' : 'none';

document.getElementById('third').style.display = style;
});

答案 2 :(得分:1)

你没有在开始后开始和关闭大括号,你已经写了

 document.getElementById('first').style.display = style;

两次,第一次应该没有

更新代码:

document.getElementById('test').addEventListener('change', function (){ 
   {
      var style = this.value == 0 ? 'block' : 'none';
      document.getElementById('none').style.display = style;
   }
   {
      var style = this.value == 1 ? 'block' : 'none';
      document.getElementById('first').style.display = style;
   }
   { 
      var style = this.value == 2 ? 'block' : 'none';
      document.getElementById('second').style.display = style;
   }
   {
      var style = this.value == 3 ? 'block' : 'none';
      document.getElementById('third').style.display = style;
   }
});

更新JsFiddle

答案 3 :(得分:1)

我就是这样做的:http://jsfiddle.net/h7zb705q/10/

var mapping = {
    0: 'none',
    1: 'first',
    2: 'second',
    3: 'third'
};

var allItems = document.querySelectorAll('div');

document.getElementById('test').addEventListener('change', function () {
     var toShow = mapping[this.value];

     for(var i = 0; i < allItems.length; i++) {
        allItems[i].style.display = 'none';
     };

     document.getElementById(toShow).style.display = 'block';
});

答案 4 :(得分:1)

使用像这样的东西

$('.statecontent').hide();
$('#myselector').change(function() {
    $('.statecontent').hide();
    $('.' + $(this).val()).show();    
});

<div class="statecontent state1">State1 Specific Page Content Goes here</div><br />
<div class="statecontent state2">State2 Specific Page Content Goes here</div><br />
<div class="statecontent state3">State3 Specific Page Content Goes here</div><br />

 <select id="myselector">
 <option value="state1">1</option>
 <option value="state2">2</option>
  <option value="state3">3</option>
 </select>

这是Fiddle

答案 5 :(得分:0)

使用jquery

是一样的

http://jsfiddle.net/h9h5858L/1/