【问题标题】:How to get the value of input fields of a particular form on which i clicked ,all the forms and their input have the same class but different value如何获取我单击的特定表单的输入字段的值,所有表单及其输入具有相同的类但值不同
【发布时间】:2020-04-14 10:39:18
【问题描述】:

我得到了由 PHP while 循环生成的所有表单,所以我不能更改它们的 id 和 classes..它们都以相同的 id 和 class 进入 DOM,但每个表单输入都有自己独特的值 ...all i想要的是,如果我单击特定表单的提交,我希望 jquery 仅获取该特定表单的输入值。

   <form  class ='myform' >
     <input type = "hidden" name ="fname" value = "sam">
     <input type = "submit" value = "Submit">
   </form>


    <form class = 'myform'>
      <input type = "hidden" name = "fname" value = "olivia">
      <input type = "submit" value = "Submit">
    </form>


    <form class = 'myform'>
      <input type = "hidden" name = " fname " value = "justien">
      <input type = "submit" value = "Submit">
    </form>

   <scirpt >

    ----here i want the solution
   </script>

【问题讨论】:

标签: javascript jquery ajax forms


【解决方案1】:

您可以在循环语句中添加一个计数器来创建 myForm。

`<form class = 'myform${counter}'>`

这将使表单具有唯一的类,但会使 CSS 变得困难。 或者,您可以选择输入字段的父项

$('input[value="justien"]').parent();

应该可以的

【讨论】:

  • 我尝试添加计数器并将总数传递给 jquery 并尝试在单击表单时执行 while 循环然后只获取子项但 .submit 不适用于循环
  • 不完全是 ,,,,我解决了它,因为我在下面发布 ,,,由我回答 ...username pr7---
【解决方案2】:

它对我来说就像一个魅力

$(document).ready(
    $(".myforms']").submit(function(e) {
        e.preventDefault();
        var f_name = $(this).children("input[name='f_name']");
        alert(f_name.val());
    })
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多