2016-06-10 40 views
0

这是我第一次使用JQuery(jquery-3.0.0.min.js)和Javascript开发。
我正在创建一个通过Web API连接到数据库的简单Web窗体。
表单包含一些输入文字标签和两个选择标签。 我使用$.ajax方法发布表单数据。
当我提交表单时,文本框中的值被正确地发布到服务器,但所选值始终为空。 这里是一块的HTML代码:

<form name="modulo" id="modulo"> 
       .... 
       <div class="field"> 
        <label id="mailLbl" for="mail">Mail:</label> 
        <input id="mail" type="text" name="mail" required="required" /> 
       </div> 

       <div class="field"> 
        <label id="CDselectLBL" for="CDselect">Centro di Costo</label> 
        <select id="CDselect" class="DropDownList" name="CDselect"></select> 

       </div> 
       <div class="field"> 
        <label id="FINALITAselectLBL" for="FINALITAselect">Finalità</label> 
        <select id="FINALITAselect" class="DropDownList" name="FINALITAselect"></select> 
       </div> 

       <div class="field"> 
        <label id="rif_studio_cliLbl" for="rif_studio_cli">Riferimento Studio Clinico:</label> 
        <input id="rif_studio_cli" type="text" name="rif_studio_cli" /> 
       </div> </form> 

..和这里是$.Ajax呼叫:

$('#modulo').submit(function (event) { 
     event.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      url: 'api/richiestes', // the local url where I want to POST 
      contentType: 'application/x-www-form-urlencoded; charset=utf-8', 
      data: $('#modulo').serialize(), 
     }) 
      // using the done promise callback 
      .done(function (data) { 
       console.log(data); 

          }); 
    }); 

这里是(从铬DevTools复制)的形式的数据:

mail:[email protected]
CD选择:10A0003 - GESTIONE FINANZIARIA
FINALITAselect:Studio clinico approvato哒Comitato Etico
rif_studio_cli:sfvsdfv

这里是请求数据的预览被发送(也来自铬DevTools复制):

邮件: “[email protected]
CDC:空
finalità:空
rif_studio_cli: “sfvsdfv”

你能帮我理解我的代码有什么问题吗?
在此先感谢您的时间和善良

+0

两个选择标记的选项值来自同一个数据库,通过Web API和$ .Ajax GET – Mauro

+0

从[jQuery.serialize()](http://api.jquery.com/serialize/)文档中,只有“成功“的控制系列化。只有选择了选项,选择控件才会成功。也许这是你的情况? –

+0

也许你也可以在发送提交之前附上你的HTML快照,这样我们就可以检查你的表单控件的状态。 –

回答

0

这是因为您没有选择控件中的任何选项元素。您需要添加选项,然后选择一个。例如

我为您创建了一个plunker代码。

https://plnkr.co/edit/L5SyOdU8hnEKdrKitmcL?p=preview

$(document).ready(function(){ 
$('form').submit(function (event) { 
    event.preventDefault(); 
    console.log($('#modulo').serialize()); 
}); 
});  

HTML是对上述意见提供由你,但我已删除的输入框,以简化页面。

希望它可以帮助你。

+0

我给选择标签加上这样的选项: $。AJAX({ 类型: “获取”, URL: “API/vCDCs”, 的contentType: “应用程序/ JSON;字符集= UTF-8”, 数据类型: “JSON”, //数据:查询, 成功:function(result){.each(result,function(i){(“#CDselect”)。append($(“”).val(result [i] .DescrizioneCentroDiCosto).text(result [I] .DescrizioneCentroDiCosto)); });} , 失败:函数(){ 警报( “错误”);} , 完成:函数(){} }); – Mauro

+0

我能够得到下拉列表的值,更新答案的细节。 – Deep