当前位置: 首页 > 知识库问答 >
问题:

如果一个条件被触发,我如何隐藏一个元素?

屠和洽
2023-03-14

嗨,我正在尝试一个html表单代码的程序,我想要隐藏一个元素,如果一个条件被触发。

我是一个新手,目前正在学习javascript。代码如下:

    <head><script>
        
        function change(object)
        {
            document.f.control.value = object.name + ", " + object.value + ", Content new";
        }
        
        function turn(object)
        {
            document.f.control.value = object.name + ", " + object.value + ", Content changed";
        }
        
        function marked(object)
        {
        var output = object.name + ", " + object.value ;
        if (object.checked)
            document.f.control.value = object.name + ", " + object.value + " marked.";
        else
            document.f.control.value = object.name + ", " + object.value + " not marked.";
        }
    
        function send()
        {
            return confirm("Do you want to send the form?");
        }
        
        function abort()
        {
            return confirm("Do you want to reset the form?");
        }
        

        
        function hideReturnFlight()
        {
            if(object.checked)
            document.getElementsByClassName('returnflight').style.visibility='hidden';
        }
    </script>
    
Form written by Igor Stefanovic<br><br>
</head>

<body>
    
    <form name="f" method="post" action="event_form_eigbsp.php" onsubmit="return send();" onreset="return abort();">
    <input name="mrmrs"        type="radio" value="Mr." onclick="change(this);" checked="checked"> Mr.
    <input name="mrmrs"       type="radio" value="Mrs" onclick="change(this);"> Mrs.<br>
    <input name="name"      value="Name" size="20" onchange="change(this);"> Name <br>
    <input name="surname"   value="Surname" size="20" onkeyup="turn(this);"> Surname<br>
    <input name="street"    value="Street" size="20"  onkeyup="turn(this);"> Street <input name="nr" value=" " size="3" onchange="change(this);"> Nr.<br>
    <input name="city"      value="City"   size="20" onchange="change(this);"> City<br>
    <input name="postalcode" type="number" value="Postal Code" size="6" onchange="change(this);"> Postal Code<br><br>
    <select name="land" onchange="change(this);">
        <option value="Switzerland" selected="selected">Switzerland</option><br>
        <option value="Germany">Germany</option>
        <option value="Austria">Austria</option>
        <option value="United Kingdom">United Kingdom</option>
        <option value="France">France</option>
        <option value="Italy">Italy</option>
        <option value="Portugal">Portugal</option>
        <option value="Spain">Spain</option>
        <option value="Ireland">Ireland</option>
        <option value="Iceland">Iceland</option>
        <option value="Netherlands">Netherlands</option>
        <option value="Belgium">Belgium</option>
        <option value="Luxembourgh">Luxembourgh</option>
        <option value="Denmark">Denmark</option>
        <option value="Finland">Finland</option>
        <option value="Sweden">Sweden</option>
        <option value="Norway">Norway</option>
        <option value="Poland">Poland</option>
        <option value="Russia">Russia</option>
        <option value="Ukraine">Ukraine</option>
        <option value="Romania">Romania</option>
        <option value="Hungary">Hungary</option>
        <option value="Czech Republic">Czech Republic</option>
        <option value="Slovakia">Slowakia</option>
        <option value="Slovenia">Slovenia</option>
        <option value="Croatia">Croatia</option>
        <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
        <option value="Serbia">Serbia</option>
        <option value="Montenegro">Montenegro</option>
        <option value="Albania">Albania</option>
        <option value="Bulgary">Bulgary</option>
        <option value="Turkey">Turkey</option>
        <option value="Israel">Israel</option>
        <option value="Belarus">Belarus</option>
        <option value="Latvia">Latvia</option>
        <option value="Lithuania">Lithuania</option>
        <option value="Estonia">Estonia</option>
        <option value="Moldova">Moldova</option>
        <option value="North Makedonia">North Makedonia</option>
        <option value="Greece">Greece</option>
        <option value="Gibraltar">Gibraltar</option>
        <option value="Malta">Malte</option>
        <option value="Monte Carlo">Monte Carlo</option>
        <option value="Liechtenstein">Liechtenstein</option>
        </select> Country of Residence<br><br>
    <input name="dateofbirth" type="date" value="Date of Birth" onkeypress="turn(this);"> Date of Birth<br><br>
        <input class="flight" name="flight" type="checkbox" value="Only One Way" onclick="marked(this);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Only One Way<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Outbound
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return Flight<br>
    <select class="outbound" name="outbound" onchange="change(this);">
        <option value="Switzerland" selected="selected">Switzerland</option><br>
        <option value="Germany">Germany</option>
        <option value="Austria">Austria</option>
        <option value="United Kingdom">United Kingdom</option>
        <option value="France">France</option>
        <option value="Italy">Italy</option>
        <option value="Portugal">Portugal</option>
        <option value="Spain">Spain</option>
        <option value="Ireland">Ireland</option>
        <option value="Iceland">Iceland</option>
        <option value="Netherlands">Netherlands</option>
        <option value="Belgium">Belgium</option>
        <option value="Luxembourgh">Luxembourgh</option>
        <option value="Denmark">Denmark</option>
        <option value="Finland">Finland</option>
        <option value="Sweden">Sweden</option>
        <option value="Norway">Norway</option>
        <option value="Poland">Poland</option>
        <option value="Russia">Russia</option>
        <option value="Ukraine">Ukraine</option>
        <option value="Romania">Romania</option>
        <option value="Hungary">Hungary</option>
        <option value="Czech Republic">Czech Republic</option>
        <option value="Slovakia">Slowakia</option>
        <option value="Slovenia">Slovenia</option>
        <option value="Croatia">Croatia</option>
        <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
        <option value="Serbia">Serbia</option>
        <option value="Montenegro">Montenegro</option>
        <option value="Albania">Albania</option>
        <option value="Bulgary">Bulgary</option>
        <option value="Turkey">Turkey</option>
        <option value="Israel">Israel</option>
        <option value="Belarus">Belarus</option>
        <option value="Latvia">Latvia</option>
        <option value="Lithuania">Lithuania</option>
        <option value="Estonia">Estonia</option>
        <option value="Moldova">Moldova</option>
        <option value="North Makedonia">North Makedonia</option>
        <option value="Greece">Greece</option>
        <option value="Gibraltar">Gibraltar</option>
        <option value="Malta">Malte</option>
        <option value="Monte Carlo">Monte Carlo</option>
        <option value="Liechtenstein">Liechtenstein</option>
        </select>&nbsp;<select class ="returnflight" name="returnflight" onchange="change(this);">
        <option value="Switzerland" >Switzerland</option><br>
        <option value="Germany">Germany</option>
        <option value="Austria">Austria</option>
        <option value="United Kingdom">United Kingdom</option>
        <option value="France">France</option>
        <option value="Italy">Italy</option>
        <option value="Portugal">Portugal</option>
        <option value="Spain">Spain</option>
        <option value="Ireland">Ireland</option>
        <option value="Iceland">Iceland</option>
        <option value="Netherlands">Netherlands</option>
        <option value="Belgium">Belgium</option>
        <option value="Luxembourgh">Luxembourgh</option>
        <option value="Denmark">Denmark</option>
        <option value="Finland">Finland</option>
        <option value="Sweden">Sweden</option>
        <option value="Norway">Norway</option>
        <option value="Poland">Poland</option>
        <option value="Russia">Russia</option>
        <option value="Ukraine">Ukraine</option>
        <option value="Romania">Romania</option>
        <option value="Hungary">Hungary</option>
        <option value="Czech Republic">Czech Republic</option>
        <option value="Slovakia">Slowakia</option>
        <option value="Slovenia">Slovenia</option>
        <option value="Croatia">Croatia</option>
        <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
        <option value="Serbia">Serbia</option>
        <option value="Montenegro">Montenegro</option>
        <option value="Albania">Albania</option>
        <option value="Bulgary">Bulgary</option>
        <option value="Turkey">Turkey</option>
        <option value="Israel">Israel</option>
        <option value="Belarus">Belarus</option>
        <option value="Latvia">Latvia</option>
        <option value="Lithuania">Lithuania</option>
        <option value="Estonia">Estonia</option>
        <option value="Moldova">Moldova</option>
        <option value="North Makedonia">North Makedonia</option>
        <option value="Greece">Greece</option>
        <option value="Gibraltar">Gibraltar</option>
        <option value="Malta">Malte</option>
        <option value="Monte Carlo">Monte Carlo</option>
        <option value="Liechtenstein">Liechtenstein</option>
        </select><br><br>
        <textarea name="comment" rows="6" cols="20" type="text" value="comment" onchange="change(this);"></textarea> Comments<br>
    <input name="control" type="text" value="control" size="50" readonly="readonly"> Control<br>
    <input type="submit" value="Send"><input type="reset" value="Reset">
    </form>
    
    <script>
        
    var flight = hideReturnFlight();
    
    if (flight = true)
        hideReturnFlight()
        
        
    </script>

</body>

我想隐藏返回航班,如果“只有一种方式”复选框被触发。

你可以忽略这个程序这里是我的服务器的链接

共有1个答案

邵阳辉
2023-03-14

在您的函数中,您可以添加可见性:隐藏,当未选中时,您可以取消设置它。这样,当用户选择单向复选框时,它就会隐藏表单字段。

null

function change(object) {
        document.f.control.value = object.name + ", " + object.value + ", Content new";
    }

    function turn(object) {
        document.f.control.value = object.name + ", " + object.value + ", Content changed";
    }

    function marked(object) {
        if (object.checked) {
            document.getElementsByClassName("returnflight")[0].style.visibility = "hidden";
            document.f.control.value = object.name + ", " + object.value + " marked.";
        }
        else {
            document.getElementsByClassName("returnflight")[0].style.visibility = "unset";
            document.f.control.value = object.name + ", " + object.value + " not marked.";
        }
    }

    function send() {
        return confirm("Do you want to send the form?");
    }

    function abort() {
        return confirm("Do you want to reset the form?");
    }
<form name="f" method="post" action="event_form_eigbsp.php" onsubmit="return send();" onreset="return abort();">
        <input name="mrmrs" type="radio" value="Mr." onclick="change(this);" checked="checked"> Mr.
        <input name="mrmrs" type="radio" value="Mrs" onclick="change(this);"> Mrs.<br>
        <input name="name" value="Name" size="20" onchange="change(this);"> Name <br>
        <input name="surname" value="Surname" size="20" onkeyup="turn(this);"> Surname<br>
        <input name="street" value="Street" size="20" onkeyup="turn(this);"> Street <input name="nr" value=" " size="3"
            onchange="change(this);"> Nr.<br>
        <input name="city" value="City" size="20" onchange="change(this);"> City<br>
        <input name="postalcode" type="number" value="Postal Code" size="6" onchange="change(this);"> Postal
        Code<br><br>
        <select name="land" onchange="change(this);">
            <option value="Switzerland" selected="selected">Switzerland</option><br>
            <option value="Germany">Germany</option>
            <option value="Austria">Austria</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="France">France</option>
            <option value="Italy">Italy</option>
            <option value="Portugal">Portugal</option>
            <option value="Spain">Spain</option>
            <option value="Ireland">Ireland</option>
            <option value="Iceland">Iceland</option>
            <option value="Netherlands">Netherlands</option>
            <option value="Belgium">Belgium</option>
            <option value="Luxembourgh">Luxembourgh</option>
            <option value="Denmark">Denmark</option>
            <option value="Finland">Finland</option>
            <option value="Sweden">Sweden</option>
            <option value="Norway">Norway</option>
            <option value="Poland">Poland</option>
            <option value="Russia">Russia</option>
            <option value="Ukraine">Ukraine</option>
            <option value="Romania">Romania</option>
            <option value="Hungary">Hungary</option>
            <option value="Czech Republic">Czech Republic</option>
            <option value="Slovakia">Slowakia</option>
            <option value="Slovenia">Slovenia</option>
            <option value="Croatia">Croatia</option>
            <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
            <option value="Serbia">Serbia</option>
            <option value="Montenegro">Montenegro</option>
            <option value="Albania">Albania</option>
            <option value="Bulgary">Bulgary</option>
            <option value="Turkey">Turkey</option>
            <option value="Israel">Israel</option>
            <option value="Belarus">Belarus</option>
            <option value="Latvia">Latvia</option>
            <option value="Lithuania">Lithuania</option>
            <option value="Estonia">Estonia</option>
            <option value="Moldova">Moldova</option>
            <option value="North Makedonia">North Makedonia</option>
            <option value="Greece">Greece</option>
            <option value="Gibraltar">Gibraltar</option>
            <option value="Malta">Malte</option>
            <option value="Monte Carlo">Monte Carlo</option>
            <option value="Liechtenstein">Liechtenstein</option>
        </select> Country of Residence<br><br>
        <input name="dateofbirth" type="date" value="Date of Birth" onkeypress="turn(this);"> Date of Birth<br><br>
        <input class="flight" id="checkbox" name="flight" type="checkbox" value="Only One Way"
            onclick="marked(this);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Only One
        Way<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Outbound
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return
        Flight<br>
        <select class="outbound" name="outbound" onchange="change(this);">
            <option value="Switzerland" selected="selected">Switzerland</option><br>
            <option value="Germany">Germany</option>
            <option value="Austria">Austria</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="France">France</option>
            <option value="Italy">Italy</option>
            <option value="Portugal">Portugal</option>
            <option value="Spain">Spain</option>
            <option value="Ireland">Ireland</option>
            <option value="Iceland">Iceland</option>
            <option value="Netherlands">Netherlands</option>
            <option value="Belgium">Belgium</option>
            <option value="Luxembourgh">Luxembourgh</option>
            <option value="Denmark">Denmark</option>
            <option value="Finland">Finland</option>
            <option value="Sweden">Sweden</option>
            <option value="Norway">Norway</option>
            <option value="Poland">Poland</option>
            <option value="Russia">Russia</option>
            <option value="Ukraine">Ukraine</option>
            <option value="Romania">Romania</option>
            <option value="Hungary">Hungary</option>
            <option value="Czech Republic">Czech Republic</option>
            <option value="Slovakia">Slowakia</option>
            <option value="Slovenia">Slovenia</option>
            <option value="Croatia">Croatia</option>
            <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
            <option value="Serbia">Serbia</option>
            <option value="Montenegro">Montenegro</option>
            <option value="Albania">Albania</option>
            <option value="Bulgary">Bulgary</option>
            <option value="Turkey">Turkey</option>
            <option value="Israel">Israel</option>
            <option value="Belarus">Belarus</option>
            <option value="Latvia">Latvia</option>
            <option value="Lithuania">Lithuania</option>
            <option value="Estonia">Estonia</option>
            <option value="Moldova">Moldova</option>
            <option value="North Makedonia">North Makedonia</option>
            <option value="Greece">Greece</option>
            <option value="Gibraltar">Gibraltar</option>
            <option value="Malta">Malte</option>
            <option value="Monte Carlo">Monte Carlo</option>
            <option value="Liechtenstein">Liechtenstein</option>
        </select>&nbsp;<select class="returnflight" name="returnflight" onchange="change(this);">
            <option value="Switzerland">Switzerland</option><br>
            <option value="Germany">Germany</option>
            <option value="Austria">Austria</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="France">France</option>
            <option value="Italy">Italy</option>
            <option value="Portugal">Portugal</option>
            <option value="Spain">Spain</option>
            <option value="Ireland">Ireland</option>
            <option value="Iceland">Iceland</option>
            <option value="Netherlands">Netherlands</option>
            <option value="Belgium">Belgium</option>
            <option value="Luxembourgh">Luxembourgh</option>
            <option value="Denmark">Denmark</option>
            <option value="Finland">Finland</option>
            <option value="Sweden">Sweden</option>
            <option value="Norway">Norway</option>
            <option value="Poland">Poland</option>
            <option value="Russia">Russia</option>
            <option value="Ukraine">Ukraine</option>
            <option value="Romania">Romania</option>
            <option value="Hungary">Hungary</option>
            <option value="Czech Republic">Czech Republic</option>
            <option value="Slovakia">Slowakia</option>
            <option value="Slovenia">Slovenia</option>
            <option value="Croatia">Croatia</option>
            <option value="Bosnia & Herzegowina">Bosnia & Herzegovina</option>
            <option value="Serbia">Serbia</option>
            <option value="Montenegro">Montenegro</option>
            <option value="Albania">Albania</option>
            <option value="Bulgary">Bulgary</option>
            <option value="Turkey">Turkey</option>
            <option value="Israel">Israel</option>
            <option value="Belarus">Belarus</option>
            <option value="Latvia">Latvia</option>
            <option value="Lithuania">Lithuania</option>
            <option value="Estonia">Estonia</option>
            <option value="Moldova">Moldova</option>
            <option value="North Makedonia">North Makedonia</option>
            <option value="Greece">Greece</option>
            <option value="Gibraltar">Gibraltar</option>
            <option value="Malta">Malte</option>
            <option value="Monte Carlo">Monte Carlo</option>
            <option value="Liechtenstein">Liechtenstein</option>
        </select><br><br>
        <textarea name="comment" rows="6" cols="20" type="text" value="comment" onchange="change(this);"></textarea>
        Comments<br>
        <input name="control" type="text" value="control" size="50" readonly="readonly"> Control<br>
        <input type="submit" value="Send"><input type="reset" value="Reset">
    </form>
 类似资料:
  • 我是石英调度器的新手。我有一个批处理文件,它将需要3分钟运行。我需要运行这批每2分钟使用石英调度器。所以我每天安排3个小时。我的问题是我需要检查第一个触发器的状态,如果它不是完整的状态,我需要从这个工作出来。我需要继续我安排的下一个工作。说明:作业53触发器在上午11.30开始,下一个触发器在上午11.32开始,下一个触发器在上午11.34开始,我需要检查上午11.30的触发器状态,如果它不是co

  • 我有“下载正在进行文件”对话框活动。当用户按下“隐藏”按钮时,活动将创建通知和隐藏进度对话框。并且当用户单击到通知时,活动显示进度对话框再次出现在活动中。我如何在按下按钮“后退”时切换活动到后退任务?

  • 问题内容: 我的html文件中有两个div。我想隐藏第一个div并在html输入按钮事件上显示另一个div 。 这是我的代码, 但这不起作用。任何帮助将不胜感激。 谢谢。 问题答案: 1)在onclick内,您不必使用暗示的“ javascript:”。 2)您检查“显示:阻止”,我总是检查“显示:无”(因为显示也可以是“行内阻止”,等等。) 尝试这个:

  • 我试图从桌面隐藏一个javascript脚本块,并且只能在移动设备上显示它。 谢谢

  • 我有一个元素在页面中间的某个地方,我希望它在顶部有一个固定的位置,当用户向下滚动时,元素到达顶部。 不知道用媒体查询能不能搞定。像@height从页面顶部90rem: }