dswmmvrg40957 2011-03-24 02:57
浏览 62
已采纳

php / jQuery根据数据库中的单选按钮设置更改是否显示/隐藏

I'm still new at jQuery and struggling. Finally, I can get jQuery to toggle show/hide when the button is clicked. What I'm still struggling with now is how to get show/hide to match up with the state of the button on page load. The value is coming from the database using php. So I don't know what it will be. I'd really appreciate some help!

Basically, if the button with id of 'specify' is checked, the text in the div shows; if the 'open' button is checked, the text is hidden. I set up the css so the specify class for the div makes it hidden. Should I have done that?

Here is the code:

HTML

<input type="radio" name="volneed" id="open" class="required" value="open to all"
 <? if($thisvolneed=='open'){echo 'checked="checked"';} ?> />
<label for="open"> Open to all </label>

<input type="radio" name="volneed" id="specify"  class="required" value="specify"
<? if($thisvolneed=='specify'){echo 'checked="checked"';} ?> /> 
<label for="specify"> Specify types of volunteers needed </label>

<div class="specify"><p>Some info...</p></div>

CSS:

.specify{display:none;}

jQuery:

$(document).ready(function() {          
    $("#specify").change(function(evt) {
    evt.preventDefault();
    $(".specify").show();
    });

    $("#open").change(function(evt) {
    evt.preventDefault();
    $(".specify").hide();
    });
});
  • 写回答

2条回答 默认 最新

  • dtl19910708 2011-03-24 03:17
    关注

    I think creating a class to hide the <div> is a fairly standard way to approach the problem. Well done! Instead of using the show or hide function on the <div>, it is just as easy to use addClass or removeClass when using the CSS approach. Also, the initial state of the <div> can be shown by using PHP to apply the class if necessary on page load.

    <div <? if($thisvolneed=='specify'){echo 'class="specify"';} ?>><p>Some info...</p></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器