【问题标题】:how to select input field in listed form with jquery如何使用jquery以列出的形式选择输入字段
【发布时间】:2011-01-17 21:22:09
【问题描述】:

我在一个页面上通过循环列出了几个表单,如下所示:(摘录)

if(mysql_num_rows($r)>0): 
while($row = mysql_fetch_assoc($r)):  ?>
        <form id="myForm" action="save_fb.php" method="post"> 

        Title: <input type="text" name="fb_title" value="<?php echo $row['fb_title']; ?>" /><br> 

    <a href="javascript:;" class="save_fb" id="<?php echo $row['fb_id']; ?>"></a>


        </form>

在我的 ajax 请求中,我执行以下操作:

  $.ajax({
   type: "POST",
   data: $("input:text[name=fb_titel]").val()+$(this).attr("id"),
   url: "save_fb.php",
   success: function(msg)
   {
    $("span#votes_count"+the_id).fadeOut();
    $("span#votes_count"+the_id).html(msg);
    $("span#votes_count"+the_id).fadeIn();
   }
  });
 });

现在我得到的结果总是第一行而不是单击链接的行但是 $(this) 工作正常但我不知道如何组合...有人知道数据线应该是什么样子吗?

感谢任何提示 =)

【问题讨论】:

    标签: jquery ajax forms jquery-selectors


    【解决方案1】:

    试试这个:

      $.ajax({
       type: "POST",
       data: $(this).prev("input[name=fb_title]").val()+$(this).attr("id"),
       url: "save_fb.php",
       success: function(msg)
       {
        $("span#votes_count"+the_id).fadeOut().html(msg).fadeIn();
       }
      });
     });
    

    【讨论】:

      【解决方案2】:

      像这样更改脚本的第三行......

      data: $(this).parent().find("input:text[name=fb_title]").val()+$(this).attr("id"),
      

      您当前的脚本会找到任何&lt;input name="fb_title"&gt; 的第一个实例。如果你使用它,它只会找到与你点击的链接形式相同的&lt;input name="fb_title"&gt;

      【讨论】:

      • 请注意,我也更正了您将“fb_titel”拼写错误为“fb_title”的错误。
      【解决方案3】:

      PHP/HTML:

      mysql_num_rows($r)>0): 
      while($row = mysql_fetch_assoc($r)):  ?>
              <form id="myForm-<?php echo $row['fb_id']; ?>" action="save_fb.php" method="post"> 
      
              Title: <input type="text" name="fb_title" value="<?php echo $row['fb_title']; ?>" /><br> 
      
          <a href="javascript:;" class="save_fb" id="<?php echo $row['fb_id']; ?>"></a>
      
      
              </form>
      

      jQuery:

        var id = $(this).attr("id"); 
        $.ajax({
         type: "POST",
         data: $("input:text[name=fb_titel]", "#myForm-" + id).val() + id,
         url: "save_fb.php",
         success: function(msg)
         {
          $("span#votes_count"+the_id).fadeOut();
          $("span#votes_count"+the_id).html(msg);
          $("span#votes_count"+the_id).fadeIn();
         }
        });
       });
      

      【讨论】:

      • 这里我得到的结果是“未定义”?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多