js获取form表单的值

输出 URL 编码的文本字符串

var o = $('form').serialize();

结果为

_token=1tBMFHKvmt93XAUNTF4WTrhezP17GWAY9YU6II4x&mall_code=135&merchant_id=2&vip_card_code=&code_coupon_name=&code_coupon_id=&start_at=&end_at=

可直接拼接在url上 如果想获取的是json对象数组 就用serializeArray()

var o = $('form').serializeArray();

结果为
js获取form表单的值_第1张图片
可以用Ajax直接将该json数组post给后台,如果需要把这些数据以字符串形式给后台,并且后台处理后使用,则要转换为json字符串形式

var a=arrayToJson(o);
document.location.href = '/manage/merchant-coupon-record/export?where='+a;
function arrayToJson(o) {
                var r = [];
                if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
                if (typeof o == "object") {
                    if (!o.sort) {
                        console.log(1)
                        for (var i in o)
                            r.push('"'+i+'"' + ":" + arrayToJson(o[i]));
                        if (!!document.all && !/^\n?function\s*toString\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                            r.push("toString:" + o.toString.toString());
                        }
                        r = "{" + r.join() + "}";
                        console.log(r)
                    } else {
                        console.log(2)
                        for (var i = 0; i < o.length; i++) {
                            r.push(arrayToJson(o[i]));
                        }
                        r = "[" + r.join() + "]";
                        console.log(r)
                    }
                    return r;
                }
                return o.toString();
            }
//后台部分
public function getExport(Request $request)
    {
        $where = $request->input('where');   //where条件
        $where=json_decode($where,true);
       $where = array_combine(array_column($where, 'name'), array_column($where, 'value'));//将数组的第一个value值作为key 第二个value值作为value 进行处理
    }

结果为

array:8 [▼
  "_token" => "1tBMFHKvmt93XAUNTF4WTrhezP17GWAY9YU6II4x"
  "mall_code" => "135"
  "merchant_id" => "2"
  "vip_card_code" => ""
  "code_coupon_name" => ""
  "code_coupon_id" => ""
  "start_at" => ""
  "end_at" => ""
]

你可能感兴趣的:(PHP)