在JavaScript中单击提交后,如何获得动态形式值?

发布于 2025-02-07 17:19:18 字数 428 浏览 1 评论 0原文

我有一个动态形式数据,该数据是从后端加载的。如果有静态形式,我可以通过document.getElementById(“ xxx”)获得值。但是,如果有动态形式未知ID,我该如何获得值?我想以这些格式提交

    "answers" :[
    {
        "questionId" : 1002,
        "response" : "Yes"
    },{
        "questionId" : 1001,
        "response" : "No"
    }
]

这是Stackblitz link

I have a dynamic form data which is loaded from backend. If there are static form, I can get value by document.getElementById("xxx"). However, if there are dynamic form which is unknown id, how can I get the value ? I would like to have these format to submit on it

    "answers" :[
    {
        "questionId" : 1002,
        "response" : "Yes"
    },{
        "questionId" : 1001,
        "response" : "No"
    }
]

Here is stackblitz link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2025-02-14 17:19:18
  1. 将您的表单字段放在表单标签中(包括提交
    按钮)。

  2. 给表格的类,例如自定义形式

  3. .custom-form selector上添加提交侦听器。

      $('。custom-form')。on('提交',功能formation umbimithandler(event){  
        //在这里添加逻辑 
    });
     
  4. 在提交处理程序中,通过做某事获取输入字段
    像这样:

      const formInputs = $(this).find(“ input”); //这将检索表单中的所有输入标签。
     

    您现在可以循环通过它们,并且
    执行您所需的操作。

  1. Put your form fields inside a form tag (including the submit
    button).

  2. Give a class to the form e.g. custom-form

  3. Add a submit listener on the .custom-form selector.

    $('.custom-form').on('submit', function formSubmitHandler(event) {  
        // add logic here 
    });
    
  4. Inside the submit handler, get the input fields by doing something
    like this:

    const formInputs = $(this).find("input");  //this will retrieve all the input tags inside the form.
    

    You can now loop through them and
    perform your desired operation.

憧憬巴黎街头的黎明 2025-02-14 17:19:18

当您在表单上循环时,您可以从后端获取的数据中生成类名称或ID,也可以构造新的名称。

When you are looping over the form, you can generate class name or id from data fetched from backend, or you can construct new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文