dongxin991209 2015-10-11 22:14
浏览 38
已采纳

jQuery - 如何将带有ajax的数组传递给PHP?

$.ajax({
            type:"get",
            url:"add/sql_customer.php",
            data:   "?title="+title+
                    "&customerid="+cid+
                    "&action=savecontract",

            success:function(data){
            }
        });

I want to post titles as an array.. How can I do that?? I get always undefined or an empty variable.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="title" name="title[]" placeholder="Titel-1"/>
<input type="text" id="title" name="title[]" placeholder="Titel-2"/>
<input type="text" id="title" name="title[]" placeholder="Titel-3"/>

</div>
  • 写回答

1条回答 默认 最新

  • douliu1092 2015-10-11 22:31
    关注

    You get the entered titles through an iteration over the input elements with class title-container:

    $('#btn-submit').click(function(){
        var titles = [];
        
        $('.title-container').each(function(){
            titles.push($(this).val());
        });
    
        console.log(titles);
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type="text" class="title-container" placeholder="Title 1"/>
    <input type="text" class="title-container" placeholder="Title 2"/>
    <input type="text" class="title-container" placeholder="Title 3"/>
    
    <button id="btn-submit">Get titles</button>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog