Javascript 无法使用jquery获取表单中选择字段的值

Javascript 无法使用jquery获取表单中选择字段的值,javascript,jquery,html,forms,jquery-selectbox,Javascript,Jquery,Html,Forms,Jquery Selectbox,我想在submit按钮上获取表单中所有元素的值。我序列化了表单中的数据,但由于某些原因,我不理解序列化数据中的select字段上select选项的值不包括在其中 这里有一个 我的表单如下所示: <form id='foo'> <p> <label>Message</label> <textarea id='message' name='message' rows='5'></textar

我想在submit按钮上获取表单中所有元素的值。我序列化了表单中的数据,但由于某些原因,我不理解序列化数据中的select字段上select选项的值不包括在其中

这里有一个

我的表单如下所示:

<form id='foo'>
      <p>
        <label>Message</label>
        <textarea id='message' name='message' rows='5'></textarea>
      </p>
      <br/>
      <p>
           <label>Name</label>
           <select id = "name">
             <option value = "1">one</option>
             <option value = "2">two</option>
             <option value = "3">three</option>
             <option value = "4">four</option>
           </select>
        </p><br/>
        <div id='success'></div>
        <button type='submit'>Send</button>
    </form>

    <p id="showdata">

    </p>
谁能帮我弄清楚我的问题在哪里!提前感谢。

如果在select元素中使用$form.serialize,则应在其中添加属性名,因为$form.serialize将从属性名中获取值

更新

将属性名称添加到选择元素

如果在选择元素中使用$form.serialize,则应在其中添加属性名称,因为$form.serialize将从属性名称获取值

更新


添加属性名称以选择元素

表单处理名称,而不是ID属性。给select元素一个名称值,它将起作用。

表单处理名称,而不是ID属性。给select元素一个名称值,它就会工作。

您的代码似乎正确,但您有一个错误。 看看select,它没有属性名。因此,$form.serialize在此元素上不起作用

这是通过添加要选择的属性名称来修复的

<form id='foo'>
  <p>
    <label>Message</label>
    <textarea id='message' name='message' rows='5'></textarea>
  </p>
  <br/>
  <p>
       <label>Name</label>
       <select name="test">
         <option value="1">one</option>
         <option value="2">two</option>
         <option value="3">three</option>
         <option value="4">four</option>
       </select>
    </p><br/>
    <div id='success'></div>
    <button type='submit'>Send</button>
</form>

<p id="showdata">

</p>

更多信息:

您的代码似乎正确,但您有一个错误。 看看select,它没有属性名。因此,$form.serialize在此元素上不起作用

这是通过添加要选择的属性名称来修复的

<form id='foo'>
  <p>
    <label>Message</label>
    <textarea id='message' name='message' rows='5'></textarea>
  </p>
  <br/>
  <p>
       <label>Name</label>
       <select name="test">
         <option value="1">one</option>
         <option value="2">two</option>
         <option value="3">three</option>
         <option value="4">four</option>
       </select>
    </p><br/>
    <div id='success'></div>
    <button type='submit'>Send</button>
</form>

<p id="showdata">

</p>

更多信息:

答案在这里:答案在这里:只需使用“复制粘贴并调试我的代码”,您将看到它不需要它,但只有“选择值”忽略调整只需使用“复制粘贴并调试我的代码”,您将看到它不需要它,但只有“选择值”忽略谢谢,这解决了我的问题。我没有注意到@John Mcthank,这解决了我的问题。我没有注意到@John Mc