dongwen7730
2014-10-02 16:38 阅读 26

在使用.each制定选项时,无法使用$ .post请求读取$ _POST值

Formulated my options string using .each and sending it via .post to an external script.

        clicked.closest("form").find("input,textarea").each(function(){
            var input=$(this);
            if(index==1){
                options = "{"+input.attr("name")+":'"+input.val()+"'";
            }else if(index==no_inputs){
                options += ","+input.attr("name")+":'"+input.val()+"'}";
            }else{
                options += ","+input.attr("name")+":'"+input.val()+"'";
            }
            index++;
        });
        alert(options);
        $.post('../pages/ajax/add_orders.php',options,function(data){
            alert(data);
        });

..problem is when my php call script is..

    <?php
        print_r($_POST);
    ?>

I simply get an empty array with no values.

The first alert shows the formulated string correctly.

But the second one returns an empty value.

Any help greatly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doushao5047 doushao5047 2014-10-02 16:55

    You're post needs to be an actual object, you're creating a string.

        clicked.closest("form").find("input,textarea").each(function(){
            var input=$(this);
            if(index==1){
                options[input.attr("name")] = input.val();
            }else if(index==no_inputs){
                options[input.attr("name")] = input.val();
            }else{
                options[input.attr("name")] = input.val();
            }
            index++;
        });
    

    I think the above way will work.

    点赞 评论 复制链接分享
  • douqiu1604 douqiu1604 2014-10-02 17:02

    try this instead:

    var options={};
    clicked.closest("form").find("input,textarea").each(function(){
        var input=$(this);
        options[input.attr("name")] = input.val();
    });
    
    点赞 评论 复制链接分享

相关推荐