[英]How to pass a variable with in jquery
我这几天才开始使用jQuery。 我喜欢它使功能变得简单的方式。 但是,因为我对使用 Javascript 非常陌生,所以我一直在使用 function 遇到障碍。
我正在尝试将几个函数绑定在一起,但我不确定我是否按照正确的顺序进行操作。 我想要它做的是从选择器href='#from=xxxxx&to=xxxxx'
获取一个变量,其中xxxxx
是使用 PHP 从数据库打印出来的值。
然后创建一个 DOM window 来显示一个表单并将这些值插入到隐藏的输入字段中。 我一直试图找出一种将这些变量从链接传递到表单的方法。
这是我的脚本:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://swip.codylindley.com/jquery.DOMWindow.js"></script>
</head>
<body>
<div id="msgBox" style="display:none"></div>
<?php $from="0"; $to="0";
for ($x=1; $x<=4; $x++){ $from++; $to++; ?>
<a href="#from=<?php echo $from;?>&to=<?php echo $to;?>" class="foo">user<?php echo $x;?></a><br>
<?php } ?>
<script type="text/javascript">
$("#msgBox").append("<form id='myForm' method='post' action='index.php'><div style='border:1px solid #cc0; width:300px;'><input type='hidden' value='<?php echo $from;?>'><input type='hidden' value='<?php echo $to;?>'>subject:<input type='text' name='subject'><br>message:<textarea class='mbox' name='msg'></textarea><br><input type='submit' value='submit'></div></form>");
$('.foo').click(function(){
$.openDOMWindow({
windowSourceID:'#msgBox',
height:135,
width:300,
overlay:0,
positionType:'anchoredSingleWindow',
windowBGColor:'#f9f5f5',
anchoredSelector:'.foo',
positionLeft:200,
positionTop:150
});
$("#msgBox").trigger();
return false;
});
</script></body></html>
在每个链接上单击事件的处理程序中,您可以获得对目标链接的引用并从 href 属性中提取值,然后设置表单中隐藏字段的值。
$('.foo').click(function() {
var href = $(this).attr('href'); // will contain the string "#from=0&to=0"
var from,to = ... // extarct from string by splitting by & or using url parse library
$('#msgBox').find("input[name='from']").val(from);
$('#msgBox').find("input[name='to']").val(to);
// rest of code...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.