2013-11-26 20 views
0

我想隐藏/显示基于如果复选框值被选中或没有一个div。找到了一些代码,做了一些定制,但现在页面每次都保持清爽。有人可以帮我吗?如果选项值被选中,显示div会不会刷新页面?

<script> 
$(document).ready(function(){ 
    $("select").change(function(){     
     if($(this).val() == '0'){ 
      $("#cp_doel_lening").attr('checked', false); 
      $("#cp_doel_lening2").attr('checked', false); 
      $("#box").hide(); 
      $(".custom_doel").hide(); 
     } 
     if($(this).val() == '7'){ 
      $("#box").hide(); 
      $(".custom_doel").show(); 
     } 
     if($(this).val() == '8'){ 
      $("#box").hide(); 
      $(".custom_doel").show(); 
     } 
     if($(this).val() == '9'){ 
      $("#cp_doel_lening").attr('checked', false); 
      $("#cp_doel_lening2").attr('checked', false); 
      $("#box").hide(); 
      $(".custom_doel").hide(); 
     } 
    }).change(); 
}); 
</script> 

我的CSS代码:

div.custom_doel { 
    display: none; 
} 

然后我的html代码:

<form><select name='scat' id='scat' class='postform' > 
    <option value='0' selected='selected'>Alle rubrieken</option> 
    <option class="level-0" value="9">Ervaringen</option> 
    <option class="level-0" value="7">Man</option> 
    <option class="level-0" value="8">Vrouw</option> 
</select><input type="submit" ></form> 

<div class="custom_doel" id="box"> 
      <strong>Doel lening</strong> 
    <li><label><input type="checkbox" name="cp_doel_lening[]" value="Particulier" id="cp_doel_lening" <?php 
    if (in_array('Particulier', $_GET['cp_doel_lening'])) { 
     echo 'checked'; 
    } 
    ?> />Particulier</label></li> 
    <li><label><input type="checkbox" name="cp_doel_lening[]" value="Zakelijk" id="cp_doel_lening2" <?php 
    if (in_array('Zakelijk', $_GET['cp_doel_lening'])) { 
     echo 'checked'; 
    } 
    ?> />Zakelijk</label></li> 
</div> 

为什么使我连续翻页刷新?我注意到,如果我从我的javascript代码中删除了.change(),那么它不会再发生,但是div将隐藏在表单搜索中(在搜索结果页面上),这是合乎逻辑的(我猜),因为css代码有一个显示:无;)。 任何帮助将不胜感激。 谢谢!

回答

0

我发了一个修复如下:我很容易检查表单是否被提交,如果是,显示器被设置为'block'。 论<div class="custom_doel" id="box">的地方我改成:

<?php 
      if (in_array('Zakelijk', $_GET['cp_doel_lening']) || in_array('Particulier', $_GET['cp_doel_lening'])) { ?> 
      <div class="custom_doel" id="box" style="display:block;"> 
      <?php } else { ?> 
      <div class="custom_doel" id="box"> 
      <?php } ?> 

此外,.change();从jquery中删除,从而导致此代码:

<script> 
$(document).ready(function(){ 
    $("select").change(function(){     
     if($(this).val() == '0'){ 
      $("#cp_doel_lening").attr('checked', false); 
      $("#cp_doel_lening2").attr('checked', false); 
      $("#box").hide(); 
      $(".custom_doel").hide(); 
     } 
     if($(this).val() == '7'){ 
      $("#box").hide(); 
      $(".custom_doel").show(); 
     } 
     if($(this).val() == '8'){ 
      $("#box").hide(); 
      $(".custom_doel").show(); 
     } 
     if($(this).val() == '9'){ 
      $("#cp_doel_lening").attr('checked', false); 
      $("#cp_doel_lening2").attr('checked', false); 
      $("#box").hide(); 
      $(".custom_doel").hide(); 
     } 
    }) 
}); 
</script> 
相关问题