Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,当文本框存储在while循环中时,如何使用jquery获取文本框值_Php_Jquery - Fatal编程技术网

在php中,当文本框存储在while循环中时,如何使用jquery获取文本框值

在php中,当文本框存储在while循环中时,如何使用jquery获取文本框值,php,jquery,Php,Jquery,使用jquery获取文本框值存在一个问题,它存储在while循环中 下面是我的设计代码 <?php while($raw=mysqli_fetch_array($result)) { ?> <input type="text" name="comment" id="comment" class="comment"/> <input type="button" name="btncmnt" id="btncmnt" class="btncmnt"

使用jquery获取文本框值存在一个问题,它存储在while循环中 下面是我的设计代码

<?php
 while($raw=mysqli_fetch_array($result))
 {
 ?>
    <input type="text" name="comment" id="comment" class="comment"/>
    <input type="button" name="btncmnt" id="btncmnt" class="btncmnt"/>
 <?php
  }
?>

这是我的jquery代码

<script type="text/javascript">
    $(document).ready(function(){
        $(".btncmnt").click(function(){                
            var comment=$(this).val(".comment");
            alert(comment);
            //this will be display only first textbox value
        });
    });
</script>

$(文档).ready(函数(){
$(“.btncmnt”)。单击(函数(){
var comment=$(this.val(“.comment”);
警惕(评论);
//这将仅显示第一个文本框值
});
});
当我单击循环的另一个按钮时,它将显示第一个texbox值 请给我ans以获取当前文本框值
提前感谢

首先请注意,当您使用相同的
id
属性生成多个元素时,您的PHP循环将创建无效的HTML。由于您似乎没有使用它,您应该删除它:

<?php while($raw = mysqli_fetch_array($result)) { ?>
  <input type="text" name="comment" class="comment"/>
  <input type="button" name="btncmnt" class="btncmnt"/>
<?php } ?>

如何以完全相同的方式获得textarea的值而不是TextBox的值。获取对元素的引用并调用
val()
我使用的bt结果将是未定义的,如何解决这个问题在这种情况下,您没有正确选择元素。我建议开始一个新问题,因为这是一个关于从相关的
输入中获取值的问题,与textarea元素无关。您的问题不完整或缺少一些语法。就像while循环中没有为textbox指定值一样。
$(".btncmnt").click(function(){                
  var comment = $(this).prev(".comment").val();
  console.log(comment); 
});