当单选按钮已设置为 yes 时,javascript 在更改下拉菜单时显示隐藏的 div

标签 javascript drop-down-menu radio-button

嗨,当我选择第二个选项时,我有一个包含不同选择的下拉菜单,有一组是/否单选按钮,一旦我选择"is",我会显示隐藏字段的 div,然后当我更改下拉列表的值时,我会隐藏这些按钮字段。问题是,当我回到下拉列表中的第二个选择时,"is"单选按钮已被选中,但隐藏字段不会像第一次那样显示,除非我单击"is"单选按钮。当单选按钮已经是"is"时,如何显示隐藏字段?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#tea").hide();
  $("#milk").hide();
                $("#coffee").hide();
$("#teano").hide();
                $("#teayes").hide();
$("#testradio").hide();


$('#q1').click(function() {
if ($(this).val() === 'y') {
  $("#testradio").show();
} else if ($(this).val() === 'n') {
  $("#testradio").hide();
} 
});



    $("select").change(function(){
        $( "select option:selected").each(function(){
            if($(this).attr("value")=="1"){
                $("#tea").hide();
  $("#milk").show();
   $("#testradio").hide();
                $("#coffee").hide();
            }
            if($(this).attr("value")=="2"){
               $("#tea").show();
   $("#testradio").hide();
  $("#milk").hide();
                $("#coffee").hide();
            }
            if($(this).attr("value")=="3"){
                $("#tea").hide();
  $("#milk").hide();
                $("#coffee").show();
  $("#testradio").hide();
            }
        });
    }).change();
  });
  </script>
   </head>
 <body>
   <div>
    <select>
        <option>Choose beverage</option> 
        <option value="1">milk</option>
        <option value="2">tea</option>
        <option value="3">coffee</option>
    </select>
</div>
<div id="milk">

 milk choices go here</div>
<div id="tea">
 caffeine
<INPUT TYPE="radio" NAME="q1" id="q1" VALUE="y">Yes
  <INPUT TYPE="radio" NAME="q1" id="q1" VALUE="n">No
 </div>
 <div id="teayes">
 tea yes
 </div>

  <div id="testradio">
 test radio button show
</div>

<div id="teano">
 tea no
</div>
<div id="coffee">
coffee goes here</div>
</body>
 </html>  

最佳答案

快速解决方案:

$('select[name="teas"]').on('change', function (e) {...}).trigger('change');

关于当单选按钮已设置为 yes 时,javascript 在更改下拉菜单时显示隐藏的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27321527/

相关文章:

css - 互联网浏览器

android - drawableRight 靠近文本时宽度匹配_parent

reactjs - 如何获取Reactjs中单选按钮(react-radio-buttons)的值?

Javascript,如何重新创建选择/范围?

javascript - 不在 html 页面上显示来自 API 的值,而是加载 JSON

javascript - dojox.enhancedGrid 插件 IndirectSelection 中的 dojo.connect

python - 如何在 tkinter 中只选择一个单选按钮

javascript - VB调用javascript资源代码背后

c# - 如何设置列表项值属性以更正数据库表行中的 ID?

css - 当内容可变时,下拉菜单选项的宽度相同