使用javascript取消隐藏和隐藏表单中的某些元素

Unhide and hide some element within the form using javascript

本文关键字:隐藏 元素 表单 javascript 取消 使用      更新时间:2023-09-26

大家好,我正在尝试隐藏和取消隐藏表单中的一些元素,例如,当单击文本区域时,必须启用发布按钮。问题是我不擅长javascript,所以我需要在这方面的帮助/指导,即使是教程的链接也可以。

这是一个包含HTML、JavaScript和jQuery的完整示例。您基本上有一个文本区域和一个隐藏(和禁用)按钮。JavaScript代码只需在文本区域添加一个点击事件,并启用表单中的按钮

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
        <script type="text/javascript"> 
            $(function(){
                $('textarea#some-textarea-id').click(function(){
                    $('button#some-button-id').show().removeAttr('disabled');
                });
            });
        </script>
    </head>
    <body>
        <form>
            <textarea id="some-text-area-id"></textarea>
            <button id="some-button-id" style="display: none" disabled>CLICK ME</button>
        </form>
    </body>
</html>

资源:

http://www.w3schools.com/jquery/
http://www.tutorialspoint.com/jquery/
https://learn.jquery.com/about-jquery/how-jquery-works/

这个问题有点模糊,但下面的代码可能会帮助您弄清楚:

$('#your_textarea_id').on('click', function(){ // add an event listener to your tag/textarea
    $('#post_button_id').removeAttr('disabled'); // removes the disable of your post button
})

如果您想隐藏/显示,只需将removeAttr("已禁用")替换为toggle(),依此类推

Refer to the code below    
$(document).ready(function(){
    $('textarea').on('click', function(){
                        $('#button-id').attr('disabled', false);
                    });
    });
where '#button-id' is the id of button you want to disable on the click event of textarea.