Found elements with non-unique id

喜欢而已 提交于 2021-02-11 15:58:05

问题


I have 3 posts on homepage and each have a comment form, I am using one form for all posts. I am using jQuery to disabled submit button if there are no text in input and also enable submit button if there is text in input. The problem is that each post submit button have same ID, how do i make button a unique id for each post ID?

 <span class="md-form">
  <form enctype="multipart/form-data" class="feeds-form form-inline md-form form-sm" method="POST" action="{% url 'site:home' %}">

  {% csrf_token %}

  <input type="hidden" value={{post.id}} name="post_comment">

  <textarea name="comment_post" class="textinput textInput" placeholder="Add a comment..." required="" id="id_comment_post{{ post.id }}" onkeyup="if(this.textLength != 0) {submit.disabled = false} else {submit.disabled = true}"></textarea>

  <button type="submit" id="submit" disabled><i class="fas fa-paper-plane" aria-hidden="true"></i></button>
  </form>

  <style>
 .feed-form .fa-paper-plane{color:grey;}
 .feed-form .fa-paper-plane:hover{color:blue;}
 .feed-form #submit[disabled]{opacity:0.5;}
  </style>

回答1:


you can try following code:

<button type="submit" id="submit-{{post.id}}" class="submit" disabled><i class="fas fa-paper-plane" aria-hidden="true"></i></button>

and in css

.feed-form .submit[disabled]{opacity:0.5;}

Updated code:

<span class="md-form">
  <form enctype="multipart/form-data" class="feeds-form form-inline md-form form-sm" method="POST" action="{% url 'site:home' %}">

 <input type="hidden" value={{post.id}} name="post_comment">

  <textarea name="comment_post" class="textinput textInput" placeholder="Add a comment..." required="" id="id_comment_post{{ post.id }}" onkeyup=""></textarea>

  <button type="submit" id="submit1" class="submit" disabled>button</button>
  </form>
  </span>

jquery:

$(document).on("keydown",".textinput",function(){
    let buttons = $(this).closest("form").find(".submit")
  if($(this).val() == "") {
  buttons.attr("disabled",true);

  }
  else{
    buttons.attr("disabled",false);
  }
});


来源:https://stackoverflow.com/questions/61729241/found-elements-with-non-unique-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!