根据选项值显示/隐藏div

时间:2014-08-10 20:06:50

标签: javascript jquery forms show-hide

我刚刚阅读了我能找到的所有其他主题,我遇到了同样的问题,但它并没有帮助我,这就是我在这里发帖的原因。

我试图根据第3个问题的选项值显示其他表单问题。 根据具有相同功能的其他主题,这应该工作得很好,但对我来说,当我改变数值并且我找不到任何错误时没有任何反应。我还是JS的新人。

提前致谢。干杯!

这是我的全部代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<link rel="stylesheet" href="style.css" type="text/css"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.js"></script>‌​

<script>

$("#aktivnost").change(function() {
    if ($(this).val() == "huddle") {
        $(".huddle_q").show();
    }

    if ($(this).val() == "1on1") {
    $(".jnj_q").show();
    }
    else {
        $(".huddle_q", ".jnj_q").hide();
    }
});

</script>

</head>

<body>

<form>

<span>Team Leader:</span> 
<select name="team_leader">
  <option value="dinko_roso"> Dinko Roso </option>
  <option value="lucija_starcevic"> Lucija Starčević </option>
  <option value="natalija_fluka"> Natalija Fluka </option>
  <option value="tamara_zdjelar"> Tamara Zdjelar </option>
  <option value="bojan_brnjac"> Bojan Brnjac </option>
  <option value="andro_vuljanic"> Andro Vuljanić </option>
</select>

<br />

<span>Ocjenjivač:</span>
 <select name="ocjenjivac">
  <option value="dinko_roso"> Branimir Spajić </option>
  <option value="lucija_starcevic"> Ozren Kovačević </option>
  <option value="natalija_fluka"> Dunja Vidak </option>
  <option value="tamara_zdjelar"> Anamaria Katić </option>
  <option value="bojan_brnjac"> Marko Nimac </option>
</select>

<br />

<span>Aktivnost:</span> 
<select id="aktivnost">
  <option value="team_meeting"> Team Meeting </option>
  <option value="1on1"> 1on1 </option>
  <option value="huddle"> Huddle </option>
</select>

<br />

<div class="huddle_q">

<span>Dužina stiskavca:</span>
<input type="radio" name="h_q1" value="0">0</input>
<input type="radio" name="h_q1" value="3">3</input>

<br />

<span>Fokusi:</span>
<input type="radio" name="h_q2" value="0">0</input>
<input type="radio" name="h_q2" value="1">1</input>
<input type="radio" name="h_q2" value="2">2</input>
<input type="radio" name="h_q2" value="3">3</input>

<br />

<span>Parkiranje:</span>
<input type="radio" name="h_q3" value="0">0</input>
<input type="radio" name="h_q3" value="3">3</input>

<br />

<span>Poticanje:</span>
<input type="radio" name="h_q4" value="0">0</input>
<input type="radio" name="h_q4" value="1">1</input>
<input type="radio" name="h_q4" value="2">2</input>
<input type="radio" name="h_q4" value="3">3</input>

<br />

<span>Predstavljanje:</span>
<input type="radio" name="h_q5" value="0">0</input>
<input type="radio" name="h_q5" value="3">3</input>

<br />

<span>Održava atmosferu:</span>
<input type="radio" name="h_q6" value="0">0</input>
<input type="radio" name="h_q6" value="1">1</input>
<input type="radio" name="h_q6" value="2">2</input>
<input type="radio" name="h_q6" value="3">3</input>

</div>

<div class="jnj_q">

<span>Dužina 1on1:</span>
<input type="radio" name="1on1_q1" value="0">0</input>
<input type="radio" name="1on1_q1" value="3">3</input>

<br />

<span>TL prepoznaje i hvali:</span>
<input type="radio" name="1on1_q2" value="0">0</input>
<input type="radio" name="1on1_q2" value="1">1</input>
<input type="radio" name="1on1_q2" value="2">2</input>
<input type="radio" name="1on1_q2" value="3">3</input>

<br />

<span>2QM:</span>
<input type="radio" name="1on1_q4" value="0">0</input>
<input type="radio" name="1on1_q4" value="1">1</input>
<input type="radio" name="1on1_q4" value="2">2</input>
<input type="radio" name="1on1_q4" value="3">3</input>

<br />

<span>Razvoj komp:</span>
<input type="radio" name="1on1_q6" value="0">0</input>
<input type="radio" name="1on1_q6" value="1">1</input>
<input type="radio" name="1on1_q6" value="2">2</input>
<input type="radio" name="1on1_q6" value="3">3</input>

</div>

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码在大多数情况下都可以正常运行。你犯的主要错误是你试图在尚不存在的元素上执行代码。您需要a)在文档末尾运行代码,或者更常见的是b)将代码放在document.ready处理程序中。

<强> jsFiddle example

$( document ).ready(function() {
   //your code here
});

请注意,在我更改下拉列表时,我添加$('.huddle_q, .jnj_q').hide();以隐藏元素的示例小提琴。