简体   繁体   English

如何在 jquery 中传递变量

[英]How to pass a variable with in jquery

I just started using jQuery in the past few days.我这几天才开始使用jQuery。 I love how it makes functions simple.我喜欢它使功能变得简单的方式。 However because I am very new to using Javascript, I keep hitting a road block with one function.但是,因为我对使用 Javascript 非常陌生,所以我一直在使用 function 遇到障碍。

I am trying to bind a couple functions together, but I'm not sure if I am doing it in the right order.我正在尝试将几个函数绑定在一起,但我不确定我是否按照正确的顺序进行操作。 What I want it to do is get a variable from a selector href='#from=xxxxx&to=xxxxx' , with the xxxxx being a value printed out from a DB using PHP.我想要它做的是从选择器href='#from=xxxxx&to=xxxxx'获取一个变量,其中xxxxx是使用 PHP 从数据库打印出来的值。

Then create a DOM window to display a form and insert those values into the hidden input fields.然后创建一个 DOM window 来显示一个表单并将这些值插入到隐藏的输入字段中。 I have been stuck trying to figure out a way to pass those variables from the link to the form.我一直试图找出一种将这些变量从链接传递到表单的方法。

Here is my script:这是我的脚本:

<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>

In the handler for the click event on each link you can get a reference to the target link and extract the values from the href attribute and then set the values of the hidden fields in the form.在每个链接上单击事件的处理程序中,您可以获得对目标链接的引用并从 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM