简体   繁体   English

单击时我的div不会使用js提交其中的表单

[英]My div when clicked doesn't submit the form inside of it with js

So the problem that I can not solve is that I have a php script that selects everything from the chat table in the db and makes it into a div with a class of 'chaty'. 所以我无法解决的问题是我有一个PHP脚本,它从数据库中的聊天表中选择所有内容,并将其变为带有“chaty”类的div。 The chaty's have an "invisible" form which has a input with the value of the chat index. chaty有一个“不可见”的形式,其输入具有聊天索引的值。 Quite confusing. 相当混乱。 So fundamentally when I click on part of the div chaty the form inside gets submitted because of a jquery onclick(only the input is displayed none). 所以从根本上说,当我点击div chaty的一部分时,由于jquery onclick(只有输入没有显示),内部的表单才会被提交。 There is a chat search which uses an ajax query it receive data on each key change on the submit and then the chats are printed out by name. 有一个聊天搜索,它使用ajax查询,它接收提交中每个键更改的数据,然后按名称打印聊天。 The chats printed out do not submit the form within them when you click. 当您点击时,打印出的聊天记录不会在其中提交表单。 Here are the chaty divs before and after and js code. 这里是前后的chaty div和js代码。 Code: Chat that gets submited: 代码:提交的聊天:

<div class="chaty">
<form method="post" action="index.php" name="chat_lox" class="chat_loc">
<input id="disspell" type="text" name="chat_locy" v 
value="5e2dbe2be3b5927c588509edb1c46f7d">
</form>
<div class="chatDesc" id="14025298">
<div class="tit">Creator: </div>
<div class="iriss"><i id="close_chatn" onclick="closeChat(14025298)" 
class="material-icons">close</i></div>

<form action="mypage.php" method="post">


<div class="authr_name"><button value="John Brown" name="userlink" 
class="subm_as_text">Hitsuji</button></div>
</form>
<div class="titd"><h3>Description</h3></div>
<div class="description_chat">jaames</div>
</div>


 <span onclick="openChat(14025298)">☰</span>
 <div class="chatname"><h3>james</h3></div>
 <div class="chatback" 
 style="background:url
 (cimages/1Hitsujib4cc344d25a2efe540adbf2678e2304c1501268980.jpg);  
 background-size: cover;
 background-repeat: no-repeat;
 background-position: 50% 50%;"></div>
 <div class="underlie"><p>Users: 2</p><p> Created: 2017/07/28 07:09:40pm</p>
 </div>

 </div>

Chat that does not get submited: 没有提交的聊天:

<div class="chaty">
<form method="post" action="index.php" name="chat_lox" class="chat_loc">
<input id="disspell" type="text" name="chat_locy" value="5e2dbe2be3b5927c588509edb1c46f7d">
</form>
  <div class="chatDesc" id="55876362" style="display: none; width: 0px;">
<div class="tit">Creator: </div>
  <div class="iriss"><i id="close_chatn" onclick="closeChat(55876362)" class="material-icons">close</i></div>

  <form action="mypage.php" method="post">


  <div class="authr_name"><button value="John Brown" name="userlink" class="subm_as_text">Hitsuji</button></div>
</form>
<div class="titd"><h3>Description</h3></div>
<div class="description_chat">jaames</div>
</div>


<span onclick="openChat(55876362)">☰</span>
<div class="chatname"><h3>james</h3></div>
<div class="chatback" style="background:url(cimages/1Hitsujib4cc344d25a2efe540adbf2678e2304c1501268980.jpg);  background-size: cover;
  background-repeat: no-repeat;
  background-position: 50% 50%;"></div>
<div class="underlie"><p>Users: 2</p><p> Created: 2017/07/28 07:09:40pm</p></div>

</div>

JS: JS:

$(".chat_loc").on("click",function() {
  $(this).submit();
  alert("submitted");
 });

Thank you 谢谢

Try swiching from .on to live. 尝试从.on切换到生活。 If it works it may means you initialize this event handler before the element is on the page. 如果它工作,它可能意味着您在页面上的元素之前初始化此事件处理程序。 On will only register events for existing elements. On将仅注册现有元素的事件。 Live will ensure the event is called for any and all that match the selector. Live将确保为所有与选择器匹配的事件调用事件。 Even those elements that are registered in the future. 即使是将来注册的那些元素。 Here is more on this. 这里有更多内容。 http://api.jquery.com/live/ http://api.jquery.com/live/

You have to use event-delegation - 你必须使用事件委派 -

$(document).on("click",".chat_loc",function() {
  $(this).submit();
  alert("submitted");
});

You may try by encapsulate the event inside the jquery ready function when the DOM loaded then the event apply to the particular element. 您可以尝试在加载DOM时将事件封装在jquery ready函数中,然后将事件应用于特定元素。

  $(function(){
    $(".chat_loc").on("click",function() {
      $(this).submit();
      alert("submitted");
    });
  });

Or you can also use on function that mentioned above answer. 或者你也可以使用上面提到的功能回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM