表单将<select>提交为undefined </select>

时间:2013-09-30 21:55:07

标签: javascript php jquery ajax forms

我在使用ajax提交的<select>中有以下<form>。 select正在提交值为:Undefined。怎么了?

<select class="form-control" name="site_theme" id="site_theme" value="<?php $result = mysqli_query($con,"SELECT * FROM settings"); while($row = mysqli_fetch_array($result)) { echo $row['site_theme']; }?>">
   <?php 
      $result = mysqli_query($con,"SELECT * FROM themes");

      while($row = mysqli_fetch_array($result))
      {
        echo "<option VALUE='".$row['theme_name']."'>".$row['theme_name']."</option>";
      }
    ?>
</select>

这里将javascript复制到丑陋的帖子,所以我在这里制作了一个jsFiddle:http://jsfiddle.net/yz5r4/

以上代码的结果如下:

<select class="form-control" name="site_theme" id="site_theme" value="Amelia">
   <option value="Amelia">Amelia</option>
   <option value="Cerulean">Cerulean</option>
   <option value="Cosmo">Cosmo</option>
   <option value="Cyborg">Cyborg</option>
   <option value="Flatly">Flatly</option>
   <option value="Journal">Journal</option>
   <option value="Readable">Readable</option>
   <option value="Simplex">Simplex</option>
   <option value="Slate">Slate</option>
   <option value="Spacelab">Spacelab</option>
   <option value="United">United</option>
</select>

1 个答案:

答案 0 :(得分:2)

你的问题首先是在ajax调用之前返回。

第二个选择错误的选择器!

以下是向您展示的示例。 http://jsfiddle.net/yz5r4/3/

您的选择器:$('input$("#site_theme")')

但应该是$("#site_theme")$("select#site_theme")

HTML:

<select class="form-control" name="site_theme" id="site_theme" value="Amelia">
   <option value="Amelia">Amelia</option>
   <option value="Cerulean">Cerulean</option>
   <option value="Cosmo">Cosmo</option>
   <option value="Cyborg">Cyborg</option>
   <option value="Flatly">Flatly</option>
   <option value="Journal">Journal</option>
   <option value="Readable">Readable</option>
   <option value="Simplex">Simplex</option>
   <option value="Slate">Slate</option>
   <option value="Spacelab">Spacelab</option>
   <option value="United">United</option>
</select>
<input type="button" id="mclick" value="click" />

JS:

// General Form Submit
$(function () {
    $('.error').hide();
    $("#mclick").click(function () {
        // validate and process form here  
        var theme = $("#site_theme").val();
        alert(theme);
    });
});