Javascript 将变量传递到新窗口
我有一个网页,其中有一些图像。检查图像将增加分数。它与下面的代码配合使用,成绩显示在页面上。现在我想要一个按钮“我的分数”来打开一个新窗口并在那里显示分数。目前它提供了一个空白页。 请提供result.html的代码,如果我当前的代码需要一些更改Javascript 将变量传递到新窗口,javascript,Javascript,我有一个网页,其中有一些图像。检查图像将增加分数。它与下面的代码配合使用,成绩显示在页面上。现在我想要一个按钮“我的分数”来打开一个新窗口并在那里显示分数。目前它提供了一个空白页。 请提供result.html的代码,如果我当前的代码需要一些更改 <html> <head> <meta content="text/html; charset=UTF-8" http-equiv="content-type"> <t
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js" ></script>
<!--*****************************************************script to count checked items Actor/Actress begin**************************************-->
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
var actor=0;
$(document).ready(function() {
$('#perk1').html(actor);
$("#img100").click(function() {
if($('#img100').is(':checked'))
actor=actor+1;
else if(actor>0)
actor=actor-1;
$('#perk1').html(actor);
});
$("#img95").click(function() {
if($('#img95').is(':checked'))
actor=actor+1;
else if(actor>0)
actor=actor-1;
$('#perk1').html(actor);
});
});
});//]]>
</script>
<!--******************************script to count checked items actor ends***************************************************************-->
<!--******************************script to save count actor begins***************************************************************-->
<script>
function myFunction()
{
window.open("result.html","","width=700,height=150,top=300,left=300");
}
</script>
<!--******************************script to save count actor ends***************************************************************-->
</head>
<!--************************************************************************body begins*************************************************************-->
<body Body Params>
<!--*******************value of actor checked clicked here begins***************************************************-->
movies you have watched: <span id="perk1"></span></br>
<!--*******************value of actor checked clicked here ends***************************************************-->
<!--**********************************************Code to share result begins*************************************************************-->
<button onclick="myFunction()">My score</button>
<!--**********************************************Code to share result begins*************************************************************-->
</br>
Please check the movies you have watched
<!--********************************************actor movies begin************************************************************************-->
<div class="imgs">
<div class="thumb">
<label for="img100"><img src="priyanka_chopra\gunday.jpg" height="200" width="275"/></label>
<input type="checkbox" class="chk " id="img100" name="img100" value="0" />
<label for="img95"><img src="priyanka_chopra\krrish3.jpg" height="200" width="275"/></label>
<input type="checkbox" class="chk " id="img95" name="img95" value="0" />
</div>
</div>
<!--********************************************actor movies end************************************************************************-->
</div>
<!--******************************************actor code ends***************************************************************************-->
</body>
<!--************************************************************************body ends*************************************************************-->
</html>
//0)
actor=actor-1;
$('#perk1').html(actor);
});
$(“#img95”)。单击(函数(){
如果($('#img95')。是(':checked'))
演员=演员+1;
else if(参与者>0)
actor=actor-1;
$('#perk1').html(actor);
});
});
});//]]>
函数myFunction()
{
window.open(“result.html”,“宽度=700,高度=150,顶部=300,左侧=300”);
}
你看过的电影:
我的分数
请检查你看过的电影
您可以将分数变量存储在隐藏字段中,并在新页面或窗口中获取隐藏字段值。更新:
window.open("result.html?score="+actor,"","width=700,height=150,top=300,left=300");
您还可以将变量作为URL传递:
比如:
在主页中:
window.open("result.html?score="+actor,"","width=700,height=150,top=300,left=300");
并通过以下方式获得分数:
在Result.html中
function geturlvar() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
用于获取URL变量:
var score = geturlvar()['score'];
希望这对您有所帮助。您可以通过查询字符串传递它
<button id="submitButton">My score</button>
$(function () {
var actor = 0;
$(document).ready(function () {
$('#perk1').html(actor);
$("#img100").click(function () {
if ($('#img100').is(':checked')) actor = actor + 1;
else if (actor > 0) actor = actor - 1;
$('#perk1').html(actor);
});
$("#img95").click(function () {
if ($('#img95').is(':checked')) actor = actor + 1;
else if (actor > 0) actor = actor - 1;
$('#perk1').html(actor);
});
$("#submitButton").on("click", function () {
alert(actor);
var url = "result.html?val=" + actor;
window.open(url, "", "width=700,height=150,top=300,left=300");
});
});
}); //]]>
我的分数
$(函数(){
var=0;
$(文档).ready(函数(){
$('#perk1').html(actor);
$(“#img100”)。单击(函数(){
如果($('#img100')是(':checked'))actor=actor+1;
如果(actor>0)actor=actor-1,则为else;
$('#perk1').html(actor);
});
$(“#img95”)。单击(函数(){
如果($('#img95')。是(':checked'))actor=actor+1;
如果(actor>0)actor=actor-1,则为else;
$('#perk1').html(actor);
});
$(“#提交按钮”)。在(“单击”上,函数(){
警觉(演员);
var url=“result.html?val=“+actor;
打开(url“,”宽度=700,高度=150,顶部=300,左侧=300“);
});
});
}); //]]>
然后在results.html页面上,您将从查询字符串中获取与此处所示类似的值 谢谢@RyanAnderson:)我没有在我的原始代码中使用var分数。变量名是actor。另外,我不明白哪个代码是main.html,哪个代码是result.html。现在单击“我的分数”ie result.html没有打开时什么也没有发生:(我看到您的代码时,请将您的
actor
设置为全局变量,或将其传递给myFunction(actor)
这将在结果中显示文本“undefined”。htmlth这将显示消息“undefined”在result.html中。我应该在result.html中使用什么代码?我正在学习js,您的代码适用于警报框。请帮助我使用result.html的代码,我无法理解您添加的链接。