简体   繁体   English

在每个内部调用jQuery的AJAX函数

[英]Calling jQuery's AJAX function inside of each

I'd like to have it so that for each button inside the table row that is clicked on, the value of the accompanying input field is fetched and sent along as data to a PHP file that executes a query. 我想拥有它,以便对于单击的表行内的每个按钮,提取附带输入字段的值并将其作为数据发送到执行查询的PHP文件。

<script type="text/javascript">
    $("tr").each(function() {
          var password = $(this +" td input").val();

          $(this +" td button").click(function(){
              $.ajax({  
                  type:"POST",  
                  url:"script.php",  
                  data:"password="+ password,
              });
          });
     });
</script>
<?php
      for($i=0;$i<10;$i++) {
           echo "<tr>
                     <td>
                         <input type='text' maxlength='15' value=''/>
                        <button>Change</button>
                     </td>
                </tr>";
      }
?>

I can't seem to select the input field and button in each row though. 我似乎无法选择每行中的输入字段和按钮。 Any ideas? 有任何想法吗?

You can't concatenate an object with a string like that and get a valid selector. 您不能将对象与类似的字符串连接起来并获得有效的选择器。

You would need to do something like either of these: 您需要执行以下任一操作:

$(this).find('td input')
$('td input', this)

In addition, the loop is unnecessary. 另外,循环是不必要的。 I would simplify like this: 我会像这样简化:

$('button').click(function (e) {
    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "script.php",
        data: "password=" + $(this).siblings('input[type=text]').val(),
    });

});

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

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