Jquery 选择器未针对 Shopify 模板上的正确 <form> 元素

Jquery selector not targeting proper <form> element on Shopify template

我很困惑!我试图在我的页面上定位一个表单并使用 jQuerys serializeArray() 函数来获取表单的所有值。

<div class="page-width">
      <header class="section-header text-center">
        <h1 class="section-header__title h2">{{ page.title }}</h1>
      </header>
           <script>
              $(document).ready(function () {
                  $( "#form" ).submit(function( event ) {
                    var fields = $( "form" ).serializeArray();
                    console.log(fields)
                    event.preventDefault();
                   });
              });
            
        </script>
  <div class="grid">
    <div class="grid__item medium-up--four-fifths medium-up--push-one-tenth">
      <div class="rte">
        {{ page.content }}
        <form action="#" method="#" id="form">
         <fieldset>
            <legend>Have you ever felt any of the below symptoms after consuming coffee or tea or other caffeinated beverage (cola, hot chocolate...) Select the boxes that apply.</legend>

            <input type="checkbox" id="heartburn" >
            <label for="heartburn">Heartburn</label><br/>

            <input type="checkbox" id="jitters" >
            <label for="jitters">Jitters</label><br/>

            <input type="checkbox" id="anxiety" >
            <label for="anxiety">Anxiety</label><br/>
           
            <input type="checkbox" id="upset-stomach" >
            <label for="upset-stomach">Upset Stomach</label><br/>
           
            <input type="checkbox" id="bathroom" >
            <label for="bathroom">Urgent need to go the bathroom</label><br/>
            
            <input type="checkbox" id="heart" >
            <label for="heart">Racing Heart</label>
         </fieldset>  
          
          
        <input type="submit" value="Submit">
      </form>
        
   
      </div>
    </div>
  </div>
</div>

现在它以一种神秘的形式登录(我认为这与 Shopifys 模板有关)

如果我将选择器更改为

            var fields = $( this ).serializeArray();

它记录为一个空数组。

问题

  1. 我要定位的随机形式是什么?
  2. 如何定位此页面上的表单?

你现在正在用页面上的这一行 $( "form" ).serializeArray(); 序列化所有表单。

将其更改为$(this).serializeArray()以便仅序列化提交的表单而不是所有表单。 (在提交范围内)

如果您想序列化它,请将名称属性添加到您的输入中。

"name" 属性在表单字段中丢失。如果您想获得所有字段值,您应该提供 "name""value" 属性是可选的。

请检查下面的工作示例。

<html>
    <head>
        <title>Submit Form</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  
    </head>
    <body>
        <div class="page-width">
            <header class="section-header text-center">
              <h1 class="section-header__title h2">{{ page.title }}</h1>
            </header>
                 <script>
                    $(document).ready(function () {
                        $( "#form" ).submit(function( event ) {
                          var fields = $( "form" ).serializeArray();
                          console.log(fields);
                          event.preventDefault();
                         });
                    });
                  
              </script>
        <div class="grid">
          <div class="grid__item medium-up--four-fifths medium-up--push-one-tenth">
            <div class="rte">
              {{ page.content }}
              <form action="#" method="#" id="form">
               <fieldset>
                  <legend>Have you ever felt any of the below symptoms after consuming coffee or tea or other caffeinated beverage (cola, hot chocolate...) Select the boxes that apply.</legend>
      
                  <input type="checkbox" name="heartburn" value="heartburn" id="heartburn" >
                  <label for="heartburn">Heartburn</label><br/>
      
                  <input type="checkbox" name="jitters" value="jitters" id="jitters" >
                  <label for="jitters">Jitters</label><br/>
      
                  <input type="checkbox" name="anxiety"  value="anxiety"  id="anxiety" >
                  <label for="anxiety">Anxiety</label><br/>
                 
                  <input type="checkbox" name="upset-stomach" value="upset-stomach" id="upset-stomach" >
                  <label for="upset-stomach">Upset Stomach</label><br/>
                 
                  <input type="checkbox" name="bathroom" value="bathroom"  id="bathroom" >
                  <label for="bathroom">Urgent need to go the bathroom</label><br/>
                  
                  <input type="checkbox" name="heart" value="heart" id="heart" >
                  <label for="heart">Racing Heart</label>
               </fieldset>  
                
                
              <input type="submit" value="Submit">
            </form>
              
         
            </div>
          </div>
        </div>
      </div>
    </body>
</html>