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

时间:2015-10-11 22:14:51

标签: javascript php jquery ajax post

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

            success:function(data){
            }
        });

我想将标题发布为数组..我该怎么做?我总是得到未定义或空变量。



<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"/>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

通过对具有类title-container的输入元素的迭代,您可以获得输入的标题:

&#13;
&#13;
$('#btn-submit').click(function(){
    var titles = [];
    
    $('.title-container').each(function(){
    	titles.push($(this).val());
    });

    console.log(titles);
})
&#13;
<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>
&#13;
&#13;
&#13;