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

时间:2015-04-09 14:05:25

标签: javascript jquery

大家好我想隐藏并取消隐藏表单中的一些元素,例如当单击文本区域时必须启用帖子按钮。问题是我对javascript不好所以我需要帮助/方向,即使是教程的链接也没关系。

3 个答案:

答案 0 :(得分:0)

问题有点模糊,但以下代码可能会帮助您解决问题:

$('#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
})

如果要隐藏/显示,则只需用toggle()替换removeAttr('disabled'),依此类推。

答案 1 :(得分:0)

这是一个包含HTML,JavaScript和jQuery的完整示例。你基本上有一个textarea和一个隐藏(和禁用)按钮。 JavaScript代码只是将一个click事件添加到textarea并启用表单内的按钮。

<!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/

答案 2 :(得分:0)

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.