dpik71879 2011-06-03 23:35
浏览 64
已采纳

如何在jquery中传递变量

I just started using jQuery in the past few days. 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.

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.

Then create a DOM window to display a form and insert those values into the hidden input fields. 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>
  • 写回答

1条回答 默认 最新

  • dongque3797 2011-06-03 23:46
    关注

    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.

    $('.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...
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等