简体   繁体   English

如何在循环生成的元素中使用PHP的jquery选择特定元素?

[英]How to select specific elements using jquery from PHPs while loop generated elements?

First of all, I'm newbie to web programming. 首先,我是Web编程的新手。 That said, I would really appreciate it if you could show me in the most understandable way to beginners. 就是说,如果您能以最易懂的方式向初学者展示给我,我将不胜感激。 I'm trying to select and edit from while loop generated lists using jQuery. 我正在尝试使用jQuery从while循环生成的列表中进行选择和编辑。 What I want is to update available members when a user clicks edit link and input another available member. 我想要的是在用户单击编辑链接并输入另一个可用成员时更新可用成员。 My code is as follows: 我的代码如下:

PHP 的PHP

       //fetch all available staffs
    $queryedit =  mysql_query("SELECT * FROM staffs WHERE Avail = 'yes'");
       while($row = mysql_fetch_assoc($queryedit)):
       $avail_staffs = $row['name'];
       $id = $row['id']; ?>
        <h4><?php echo $avail_staffs; ?></h4>
         <!--form is the child of <a> -->
        <a href='#' class="edit-<?php echo $id; ?>">Edit
                 <!--Hidden form that appears when I want to edit Staffs->
           <form id="hidden_form" style="display:none;">
               <input type="text" class="staff-name" placeholder="Enter Staff name">
               <button id="edit-<?php echo $id?>">update</button>
           </form>
       </a>
           <?php endwhile ?> 

Then I tried to handle them in jQuery like this: 然后,我尝试像这样在jQuery中处理它们:

jQuery jQuery的

        $(".edit").click(function () {
          //display hidden form
         $(this).find('form').show();
         //when the previously hidden button is clicked, grab the value in the input field & send to update.php
         $(this).find('button').click(function () {
          var staffs = $("input=[class='staff_name']").val();
           $.post("internal/update.php",{staffs:staffs},function(data) {
             alert("Updated");
              }); 

           });
          });

But this only updates the first name. 但这只会更新名字。 It only applies change to the first member name even when I click the last one. 即使单击最后一个,它也只会将更改应用于第一个成员名称。

You can do something like this: 您可以执行以下操作:

$(".edit").click(function () {
    //display hidden form
    $(this).find('form').show();
    //when the previously hidden button is clicked, grab the value in the input field & send to update.php
    $(this).find('button').click(function () {
        var input = []; // create a new empty array 

        $('#hidden_form').each($('input'), function(){ // loop over each of the inputs
            input.push($(this).val()); // grab input value and add it to the array
        });
        $.post("internal/update.php",{input:input},function(data) {
            alert("Updated");
        });
    });
});

Haven't tested it, so there may be a few kinks here and there. 还没有测试,所以可能在这里和那里有一些纠结。

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

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