js函数跳转页面时的参数传递

  • Post author:
  • Post category:其他


有两种方式;

📢第一种,直接将参数存入session会话中;


window.sessionStorage.setItem("参数",值);


例如:

 //点击修改链接时触发;考虑需要携带此学生的学号/id;
function toUpdate(sid){
    alert(sid);
    //在转出页面前就把要更新的ID号存入session;
    window.sessionStorage.setItem("sid",sid);
    //跳转到更新页面;
    location.replace("updateStudent.html");
}

然后在另一边取出;


var upsid = window.sessionStorage.getItem("参数");

//先从session中获得要更新的Id号==>sid;
var upsid = window.sessionStorage.getItem("sid");

📢另一种方式,直接在url中拼接参数;


location.replace("updateCheck.html?参数="+值+"&参数="+值);

//点击修改链接时触发;携带学号信息;以及考勤ID信息;
function toUpdate(checkId,checkSno){
	//转到更新页面;
	location.replace("updateCheck.html?checkId="+checkId+"&sno="+checkSno);
}

另一方截取字符串即可;

例如:

//由传递的url得到参数;
var checkId = (location.search).substring(((location.search).indexOf("=")+1),(location.search).indexOf("&"));
var sno = (location.search).substring(((location.search).lastIndexOf("=")+1),(location.search).length);