$(this).closest('h5')。find('。comment')。text返回“”(无)

时间:2019-05-10 18:52:10

标签: jquery html

此代码可在我的其他网站上使用(并且是主流答案):  var qtd = $(this).closest('tr').find('select').val();

但是,我正在做类似的事情,不断返回空白或未定义:

$(this).closest('.comment').find('h5').text(); //returns "" $(this).closest('h5').find('.comment').text(); // returns "" $(this).closest('h5').find('.comment').val(); //returns undefined

我还尝试了'p'标签,而不是认为这可能是“ CSS”问题:

info= $(this).closest('p').find('.comment').text(); //returns ""

编辑:这是我的服务器端PHP代码,它很可能是PHP问题(因为没有人响应,我假设我的jQuery是正确的):

while($row = mysqli_fetch_assoc($records)) { ?>

<h5 class="comment bold"> <?php echo $row['comment']; ?> </h5></div>

<?php
}
?>
</div>

完整的JS / AJAX代码(根据要求)。这样可以更好地了解我在做什么。

<script>
  $(document).ready(function() {
 $('.update-btn').click(function() {
  var edit_id= $(this).data("id");
  //var qtd = $(this).closest('tr').find('select').val(); //works
 //  var new_info= $(this).closest('.comment').find('h5').text(); // returns ""
  var new_info= $(this).closest('p').find('.comment').text(); // returns ""
// var new_info= $(this).closest('h5').find('.comment').val(); //returns undefined

 alert(new_info);
  $.ajax({
  type: "POST",
  url: 'update.php',
  data:{ 
new_info, new_info,
edit_id, edit_id,
  },
  success: function(data){
          alert(data); 
              if(data==1) {
                  alert("Updated");
                  // $('#comments-all').load("#comments-all");
              }if(data=='0'){
                  alert('wrong');
          }    
          } //end success
  }); //end ajax
 }); 
        }); //end doc

0 个答案:

没有答案