简体   繁体   English

我如何使用while循环通过jQuery获取输入字段值

[英]How can i get input field value using while loop through jquery

I am using php while loop and want to get value for ajax. 我使用的是while循环php,并且希望获取有关ajax的价值。 my code is 我的代码是

while{
<form class="commentform">
  <input type="hidden" class="proid" name="proid[]" value="<?=$rr['id']?>">  
  <input type="text" class="form-control wac" name="comval[]" placeholder="Write a comment..">
  </form>  
}
<script type="text/javascript">
$(document).ready(function(){
    $('.commentform').on('submit', function(e){
        var comment = $(this).next('.proid').attr('value');
        $('#res').html(comment);
        alert(comment);
        return false;
<script>

please guide to get value in while loop for ajax on form submit. 请指导在表单提交的ajax的while循环中获取价值。 Thanks in advance 提前致谢

You need to use .find() / .children() as input is a child not a sibling. 您需要使用.find() / .children()作为input是一个孩子没有兄弟姐妹。 Additionally use .val() to get the current value 另外使用.val()获取当前值

var comment = $(this).find('.proid').val();

instead of 代替

var comment = $(this).next('.proid').attr('value');

You could also serialize all the inputs of the current form and run through each one like this : 您还可以序列化当前表单的所有输入,并像这样遍历每个输入:

$('.commentform').on('submit', function(e){
    var formInputs = jQuery(this).serializeArray();
    jQuery.each(formInputs, function(i, field){
        console.log(field.name + " : " + field.value);
    });
});

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

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