簡體   English   中英

如何獲取我單擊的特定表單的輸入字段的值,所有 forms 及其輸入具有相同的 class 但值不同

[英]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

我得到了由 PHP while 循環生成的所有 forms,所以我無法更改它們的 id 和類..它們都以相同的 id 和 class 進入 DOM,但每個表單輸入都有自己的不同值... ,如果我單擊特定表單的提交,我希望 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>

您可以在循環語句中添加一個計數器來創建 myForm。

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

這將使 forms 具有獨特的 class 但會使 CSS 變得困難。 或者,您可以 select 輸入字段的父級

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

那應該工作

它對我來說就像一個魅力

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM