[英]use popupWindow = window.open with javascript variables
我有以下脚本:
<script type="text/javascript">
function train(roww){
$.post ('getpeopleinjobs.php',{
postvarposition: form["position"+roww].value,
postvarjob: form["job"+roww].value,
postvarperson: form["person"+roww].value,
postrow: roww},
function(output){
popupWindow = window.open('trainingneeded.php?position=postvarposition&job=postvarjob&person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
我已经测试了脚本,并且可以100%运行。 我的问题是我的变量postvarposition,postvarjob和postvarperson是作为URL字符串中的文本而不是实际变量传递的。
我如何格式化行
popupWindow = window.open('trainingneeded.php?position=postvarposition&job=postvarjob&person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
以便变量作为变量而不是文本传递。
提前致谢。
您需要将变量连接到字符串中:
'trainingneeded.php?position=' + postvarposition + '&job=' + postvarjob + '&person=' + postvarperson
编辑:
使用您的代码,您可以看到.post
调用由三部分组成:URL(getpeopleinjobs.php),要传递的参数(用{}括起来)和成功处理程序(函数)。 它们中的每一个在范围上都是分开的,因此它们都无法访问其他任何一个。 基本上,您的成功处理程序不知道什么是“后置”。 为了使其可见,您必须将变量带到其容器范围之外。 在这种情况下,您必须从{}中删除这些项目的分配,并将其放置在.post
调用方法之外。
<script type="text/javascript">
function train(roww){
$.post ('getpeopleinjobs.php',{
// Because this is inside a {} these variables
// can be considered "trapped" inside this scope
postvarposition: form["position"+roww].value,
postvarjob: form["job"+roww].value,
postvarperson: form["person"+roww].value,
postrow: roww},
function(output){
popupWindow = window.open('trainingneeded.php?position=postvarposition&job=postvarjob&person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
一个可能的解决方案是:
<script type="text/javascript">
function train(roww){
// declare them and assign them out here
var position = form["position"+roww].value;
var job = form["job"+roww].value;
var person = form["person"+roww].value;
$.post ('getpeopleinjobs.php',{
postvarposition: position,
postvarjob: job,
postvarperson: person,
postrow: roww },
function(output){
popupWindow = window.open('trainingneeded.php?position=' + position + '&job=' + job + '&person=' + person,'popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
});
}
</script>
我能够使它工作,确实是必需的字符串连接。
//some data taken from the google maps api.
resstring=results[0].geometry.location;
var new_window = 'http://www.trystingtrees.com/coordsc.php?coords='+resstring+'&listing_id=".$listing_id."';
popupWindow=window.open(new_window,'_SELF');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.