dongxun2903 2016-08-15 08:41
浏览 60

第一个元素在第一次调试中没有同时出现第二个元素

Here is my code. Please kindly help me because I don't have any basic Javascript programming. The second element and button did not appear if user NOT SELECT OTHERS from first drop down list. Then the selection couldn't submit because the button didn't appear. If user select OTHERS from first drop down list, the second element and button will be appear or visible. What is causing this?

<html>
<head>
<title></title>
<script>
function checkchange() {
    if (document.getElementById('favouritecolour').value == 'OTHERS') {
        document.getElementById('other').style.display='block';
    } else {
        document.getElementById('other').style.display='none';
    }
};


function check() {
    if (document.getElementById('pets').value == 'OTHERS') {
       document.getElementById('besides').style.display='block';
    } else {
        document.getElementById('besides').style.display='none';
    }
};
</script>
</head>
<body>
<select id='favouritecolour' onChange='checkchange()'>
    <option value='BLUE'>BLUE</option>
    <option value='RED'>RED</option>
    <option value='OTHERS'>OTHERS</option>
</select>
<div id='other' style="display: none">
    <input type='text' placeholder="FILL IN"/><br/>
    <select id='pets' onChange='check()'>
        <option value='DOG'>DOG</option>
        <option value='RABBIT'>RABBIT</option>
        <option value='OTHERS'>OTHERS</option>
</select>
<div id='besides' style="display: none">
    <input type='text' placeholder="FILL IN"/>
    <input type="submit" name="SUBMIT" />
</div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongshungou7699 2016-08-15 09:01
    关注

    <html>
    <head>
     <title></title>
     <script>
     function checkchange() {
      if(document.getElementById('favouritecolour').value === 'OTHERS') {
        document.getElementById('other').style.display='block';
    }else {
        document.getElementById('other').style.display='none';
                document.getElementById('pets').value = "DOG"; // Reset the value to Dog 
                document.getElementById('besides').style.display='none'; // You need to hid this one two this will fix your issue
            }
        };
    
    
        function check() {
            if(document.getElementById('pets').value === 'OTHERS') {
                document.getElementById('besides').style.display='block';
            }else{
                document.getElementById('besides').style.display='none';
            }
        };
        </script>
    </head>
    <body>
    
        <select id='favouritecolour' onChange='checkchange()'>
            <option value='BLUE'>BLUE</option>
            <option value='RED'>RED</option>
            <option value='OTHERS'>OTHERS</option>
        </select>
        <div id='other' style="display: none">
            <input type='text' placeholder="FILL IN"><br />
            <select id='pets' onChange='check()'>
                <option value='DOG'>DOG</option>
                <option value='RABBIT'>RABBIT</option>
                <option value='OTHERS'>OTHERS</option>
            </select>
        </div>
        <div id='besides' style="display: none">
            <input type='text' placeholder="FILL IN">
        </div>
        <input type="submit" name="SUBMIT" id="submit"/>
    </body>
    </html>

    First of all, close all html tags! Second, mostly its better to check for "===" instead of "==" in JS (google about why). Third and finnaly, you need to hide the element

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?