dongli1920 2017-02-04 03:11
浏览 77

没有文本,Jquery会阻止输入框触发但不会阻止textarea触发器

My input box uses placeholder text and jquery to ensure it doesn't trigger until I add text (i.e. not placeholder text). I want to replace the input box with a text area but cannot get the jquery to work. With the textarea below, the submit button triggers regardless of whether I type in the text area.

<div class="RepForm-loggedin" >
  <form name="RepForm" method="POST" action="<?php echo $this->newReply;?>">
    <table name="RepFormTable" style="width:100%">
      <tr>
        <td style="width:100%">

    //CURRENT INPUT BOX
        <input type="text" rows="8"  id="repContent" class="inputbox inputbox-title placeholder-soc-med" name="repContent" placeholder="Ask a question . . ."/>

    //DESIRED REPLACEMENT TEXT AREA
        <textarea type="text" rows="8"  style="max-width:600px; width:100%" id="repContent" class="inputbox inputbox-title placeholder-soc-med" name="repContent" placeholder="Ask a question . . ."></textarea>

        </td>
       </tr>
       <tr>
         <td style = "text-align:right;">
             <input type="submit" value="Submit" id="sendComment" class="dgrey-button">
         </td>
       </tr>
    </table>
  </form>
</div>

<script type="text/javascript">
        $(document).ready(function(){
            $('#sendComment').attr('disabled',true);
            $('#repContent').keyup(function(){
                if($(this).val().length !=0)
                    $('#sendComment').attr('disabled', false);
                else
                    $('#sendComment').attr('disabled',true);
            })
        });
    </script>
  • 写回答

1条回答 默认 最新

  • dongyan7988 2017-02-04 03:21
    关注

    You have duplicated id="repContent".

    Your input box also has this ID. So, when you select your element with $('#repContent'), it is returning only your input field.

    Just remove this attribute from the <input /> and your code will work.

    Working fiddle: https://jsfiddle.net/mrlew/7o493syd/1/


    You may ask: Why $('#id') does not select all elements with this ID?

    First of all, you can have only one element with the same ID in the document. From the HTML specification:

    id = This attribute assigns a name to an element. This name must be unique in a document.

    Then, when you select by id using jQuery, it will return just the first match (because it uses document.getElementById() internally). From the docs:

    Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀