Jquery param()方法

Jquery param()方法

jQuery的
param()方法允许我们创建对象或数组的序列化表示。发出AJAX请求时,我们可以在URL字符串中使用这些序列化值。
此方法具有
obj 和
trad 这两个参数,其中第一个是必需的,第二个参数对于使用。

语法

 $.param(obj, trad)
此方法的参数值定义如下。

参数值

obj: 。可以是数组或要序列化的对象。
trad: 此可选参数是一个布尔值,用于指定是否使用传统样式的param。
让我们看一些插图,以了解如何使用
param()方法。

Example1

在本示例中,我们使用param()方法创建对象的序列化表示。在这里,有一个名为
学生的对象,其中包含一些学生的详细信息。我们正在对
student 对象执行
param()方法以创建其序列化表示形式。
 <!DOCTYPE html>
<html>
<head>
<title> jQuery param() </title>
<script src="/ajax/libs/jquery/3.5.1/jquery.min.js"></script></head>
<body>
<h1> Welcome to the bianchenghao6.com </h1>
<h2> This is an example of using the jQuery's param() method. </h2>
<p> Click the following button to see the effect. </p>
<button> Click me</button>
<p id = "para"></p>
<script>
$(document).ready(function() {
student = new Object();
student.Name = "John";
student.Rollno = "19";
student.Standard = "Fourth";
$("button").click(function() {
$("#para").text($.param(student)).css("fontSize", "23px");
});
});
</script>
</body>
</html>
输出

Welcome to the bianchenghao6.com

This is an example of using the jQuery's param() method.

Click the following button to see the effect.

Example2

在此示例中,存在一些复杂的对象。我们正在这些复杂对象上实现
param()方法以查看结果。在这里,我们使用
decodeURIComponent 显示已解码的对象。
此处,有两个对象
obj1 和
obj2 。我们正在这些对象上实现
param()方法,并以解码格式显示结果。
 <!DOCTYPE html>
<html>
<head>
<title> jQuery param() </title>
<script src="/ajax/libs/jquery/3.5.1/jquery.min.js"></script></head>
<body>
<h1> Welcome to the bianchenghao6.com </h1>
<h2> This is an example of using the jQuery's param() method. </h2>
<p> Click the following button to see the effect. </p>
<button> Click me</button>
<p id = "p1"></p>
<p id = "p2"></p>
<script>
$(document).ready(function() {
var obj1 = new Object ({
a: [ 1, 2, 3 ]
});
var obj2 = new Object ({
a: { b: 1, c: 2, d: 3}, e: [ 4, 5]
});
$("button").click(function() {
var res1 = decodeURIComponent( $.param(obj1));
$("#p1").text(res1);
var res2 = decodeURIComponent( $.param(obj2));
$("#p2").text(res2);
});
});
</script>
</body>
</html>
输出

Welcome to the bianchenghao6.com

This is an example of using the jQuery's param() method.

Click the following button to see the effect.