簡體   English   中英

如何在 jquery 中傳遞變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM