简体   繁体   English

需要帮助jquery

[英]Need help in jquery

I have following code: 我有以下代码:

<div id="comments" class="clearfix">
  <div class="conversation box_round_s box_shadow clearfix mtm">
  <div class="conversation box_round_s box_shadow clearfix mtm">
    <input class="conv_tracker" type="hidden" value="4695f1db2d">
    <div class="conv-header">2</div>
    <div class="comment pts plm prs pbs">
    <div class="comment plxl pts plm prs pbs">
    <div class="replybox pvs clearfix">
      <textarea class="comment_txt_r fses fft"></textarea>
    </div>
  </div>
  <div class="conversation box_round_s box_shadow clearfix mtm">
  <div class="conversation box_round_s box_shadow clearfix mtm">
</div>

and I'm trying to read the value of input with class conv_tracker (4th line in code with value="4695f1db2d"). 我试图用类conv_tracker读取输入的值(代码中的第4行,值=“4695f1db2d”)。 I want to read this value. 我想读这个值。 I'm reading it when someone presses enter in textarea. 当有人按下textarea时,我正在阅读它。 there are multiple text areas on page. 页面上有多个文本区域。 I'm using following jquery: 我正在使用以下jquery:

the code does enter inside the if, but shows undefined. 代码确实输入if,但显示未定义。

$(".comment_txt_r").keydown(function(e){
  var code = (e.keyCode ? e.keyCode : e.which);
  if(code == 13) {      
    var ctext = $(this).val();
    var relid = $(this).closest('.conversation').children('input.conv_tracker').val();
    alert(relid);
  }
});

can anybody please tell me what I'm doing wrong? 任何人都可以告诉我我做错了什么?

Use $('#conv_tracker').val() . 使用$('#conv_tracker').val() The ID must be unique within the page, so there is no reason to traverse up and down the DOM via closest and children , and then select by ID. ID必须在页面中是唯一的,因此没有理由通过closestchildren遍历DOM,然后按ID进行选择。 Just select by ID. 只需按ID选择。

It's very likely that if your current code isn't working, the ID is not unique within the page, and it's being stripped off the later elements with duplicate IDs preventing your code from selecting the element. 很可能如果您当前的代码不起作用,则该ID在页面中不是唯一的,并且它会被重复的ID从后面的元素中剥离,从而阻止您的代码选择该元素。 If this is the case, you need to use a class or some other attribute for the repeated elements. 如果是这种情况,则需要为重复元素使用类或其他属性。


As an unrelated suggestion, try using data attributes and attaching the value directly to the element you're binding the events to, rather than dumping random, uncoupled, semantically meaningless elements into the DOM. 作为一个不相关的建议,尝试使用data属性并将值直接附加到您要绑定事件的元素,而不是将随机,非耦合,语义无意义的元素转储到DOM中。

Your text area gets a data-conv-tracker attribute: 您的文本区域获取data-conv-tracker属性:

<textarea class="comment_txt_r fses fft" data-conv-tracker="4695f1db2d" />

And your code is simplified drastically with no additional selection/DOM traversal: 并且您的代码大大简化,没有额外的选择/ DOM遍历:

if(code == 13) {      
  var ctext = $(this).val();
  var relid = $(this).data('conv-tracker');
  alert(relid);
}

change the line 换线

var relid = $(this).closest('.conversation').children('input#conv_tracker').val();

to

var relid = $('#conv_tracker').val()

You are using $(this) in the wrong context (it is $('.comment_txt_r') in your code) 您在错误的上下文中使用$(this) (代码中为$('。comment_txt_r'))

Just address the input directly in jQuery like this $('#conv_tracker').val() . 只需在jQuery中直接解决输入,如$('#conv_tracker').val() So your code would look like: 所以你的代码看起来像:

$(".comment_txt_r").keydown(function(e){
  var code = (e.keyCode ? e.keyCode : e.which);
  if(code == 13) {      
    var ctext = $(this).val();
    var relid = $('#conv_tracker').val();
    alert(relid);
  }
});

HTML HTML

Replace <textarea class="comment_txt_r fses fft" /> 替换<textarea class="comment_txt_r fses fft" />

With <textarea class="comment_txt_r fses fft"></textarea> 使用<textarea class="comment_txt_r fses fft"></textarea>

JavaScript JavaScript的

$('#comments .conversation').each(function(i, conversation){    
    $('.comment_txt_r', conversation).keydown(function(e){
      if((e.keyCode ? e.keyCode : e.which) == 13) {      
        var ctext = $(this).val();
        var relid = $('.conv_tracker', conversation).val();
        alert(relid);
      }
    });
    // additional per conversation code
});

By running through each conversation individually you give jQuery a smaller group of elements to filter through. 通过逐个遍历每个会话,您可以为jQuery提供一小组元素来过滤。

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

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