dongxin991209
2015-10-11 22:14
浏览 35

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>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题