通过输入标签的 html 值字段将 javascript 变量传递到另一个页面


Passing a javascript variable to another page through html value field of input tag

下面是我获取行ID的代码:

<script>
function myFunction(x){
alert("row idex="+x.rowIndex);
var rowID=x.rowIndex;
}
</script>

现在我想通过 HTML 的输入标签将此行 ID 传递给另一个页面

<input type="hidden" name="rowid" value="here i need to pass the javascript variable that contain row id vale" >

我是这里的初学者,因此详细的解释将不胜感激。

试试这个

<script type="text/javascript">
function myFunction(x){
alert("row idex="+x.rowIndex);
var rowID=x.rowIndex;
document.getElementById("rowid").value = rowID
}
</script>

将输入更改为

<input type="hidden" name="rowid" id="rowid" value="" >

当您浏览页面时,您的 js 脚本会在每次页面加载时刷新。所以你不能直接将js变量从一个页面传递到另一个页面。相反,您可以在一个页面上设置 cookie 并在下一页上检索该 cookie。

document.cookie="username=test; expires=Thu, 18 Dec 2013 12:00:00 UTC";
var x = document.cookie;
<script type="text/javascript">
//This function will set the cookie
function myFunction(x){
alert("row idex="+x.rowIndex);
var rowID=x.rowIndex;
var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
document.cookie = "row =" + rowID + "; " + expires;
}
//To retrieve the set cookie value call following function
function get_cookie_value()
{
return username=getCookie("row");
}
</script>

JavaScript 不提供那种功能来将 varibale 的值传递给其他页面,尽管您可以通过将值存储在浏览器的本地存储中并在其他页面上获取该值来做到这一点。

不需要 html 元素来传递 js 的变量。

试试这个

第一页上的JS:要从中发送数据的位置

//suppose you want to pass 25 as value of variable rowid
var rowid = 25;
//now store rowid variable in your browser
//window.localStorage.setItem('name to get and set value',your variable);
window.localStorage.setItem('rowid',rowid);

JS在其他页面上:您要使用该数据的位置

//now get that rowid variable in your other page
//window.localStorage.getItem('name to get and set value');
rowid_value = window.localStorage.getItem('rowid');
alert(rowid_value);