doulan8330 2017-02-17 01:07
浏览 136
已采纳

如果textarea为空则隐藏按钮的Javascript函数不起作用

I have a textarea inside a div with a select dropdown underneath, and underneath that are two buttons. One is to preview, and the other is going to be for Uploading the text to a file. I am trying to hide the Upload button if the textarea is empty. My javascript is not working since it always hides the button.

Here is the form, button container, and select dropdown (with all of the options left out since there are 20 of them):

<div class="container">
<label for="text-area">Paste your code here: </label>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <textarea name="code_input" id="code_textarea" class="form-control" rows="15" placeholder="Start coding!" required><?= isset($_POST['code_input']) ? $_POST['code_input'] : '' ?></textarea>
    <div class="button-container">
        <select required name="language-select" class="form-control" id="language_selector">
            <option value="" selected disabled>Language</option>
            <option>20 options follow</option>
<!-- THIS WILL KEEP THE VALUE IN THE DROPDOWN AFTER SUBMIT -->
            <script type="text/javascript">
                document.getElementById('language_selector').value = "<?php echo $_POST['language-select'];?>";
            </script>
        </select>
        <br/>
        <br/>
        <div id="button-container" class="btn-toolbar">
            <button id="drive_submit_btn" class="btn btn-md" type="submit">Preview</button>
            <button id="upload_btn" class="btn btn-md" type="submit">Upload</button>
        </div>
    </div>
</form>
<div class="show-code">
    <script src="lib/prism.js"></script>
    <!-- THIS DISPLAYS THE CODE AFTER SUBMITTING USING THE PRISM.JS PLUGIN FOR SYNTAX HIGHLIGHTING -->
    <!-- Get language selection from dropdown and append it to language class. Echo the text as highlighted code -->
    <pre><code class="language-<?php echo $language ?>"><?php echo $user_code; ?></code></pre>
</div>
    </div>


<!-- THIS IS THE JAVASCRIPT TO HIDE THE BUTTON UNTIL TEXT IS ENTERED.-->
<!-- HOWEVER IT ALWAYS HIDES IT!! -->

<script>
$(document).ready(function() {
 /* I EVEN TRIED TO TRIM IT TO NO AVAIL */
    var content = $.trim($('#code_textarea').val());
    if(content.length === 0) {
        $('#upload_btn').hide();
    } else {
        $('#upload_btn').show();
    }
});
</script>

I've seen so many answers that link to their JSFiddle, and they all work fine. What is causing mine not to work correctly? Everything else works fine except for my JS function.

  • 写回答

2条回答 默认 最新

  • doumei9589 2017-02-17 01:14
    关注

    Attach your handler to textarea's 'keyup' event. (And start with invisible btn.)

    $(document).keyup('.code_textarea',function() {
     /* I EVEN TRIED TO TRIM IT TO NO AVAIL */
        var content = $.trim($('#code_textarea').val());
        if(content.length === 0) {
            $('#upload_btn').hide();
        } else {
            $('#upload_btn').show();
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
    <label for="text-area">Paste your code here: </label>
    <form method="post" >
        <textarea name="code_input" id="code_textarea" class="form-control" rows="5" placeholder="Start coding!" required></textarea>
        <div class="button-container">
            <select required name="language-select" class="form-control" id="language_selector">
                <option value="" selected disabled>Language</option>
                <option>20 options follow</option>
    <!-- THIS WILL KEEP THE VALUE IN THE DROPDOWN AFTER SUBMIT -->
                <script type="text/javascript">
                    document.getElementById('language_selector').value = "";
                </script>
            </select>
            <br/>
            <br/>
            <div id="button-container" class="btn-toolbar">
                <button id="drive_submit_btn" class="btn btn-md" type="submit">Preview</button>
                <button style="display:none" id="upload_btn" class="btn btn-md" type="submit">Upload</button>
            </div>
        </div>
    </form>
    <div class="show-code">
        <script src="lib/prism.js"></script>
        <!-- THIS DISPLAYS THE CODE AFTER SUBMITTING USING THE PRISM.JS PLUGIN FOR SYNTAX HIGHLIGHTING -->
        <!-- Get language selection from dropdown and append it to language class. Echo the text as highlighted code -->
        <pre><code class="language-"></code></pre>
    </div>
        </div>
    
    
    <!-- THIS IS THE JAVASCRIPT TO HIDE THE BUTTON UNTIL TEXT IS ENTERED.-->
    <!-- HOWEVER IT ALWAYS HIDES IT!! -->

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码