开发者

Passing variable in Javascript

开发者 https://www.devze.com 2023-01-05 09:26 出处:网络
What is wrong with this code? <script type=\"text/javascript\"> var urlquery = location.href; var urlparts = urlquery.split(\'=\');

What is wrong with this code?

<script type="text/javascript">

   var urlquery = location.href;
   var urlparts = urlquery.split('='); 
   var urlplan  = (urlparts[1]);      

   $(document).ready(function() {
      $开发者_JS百科('#LDF a').click(function() {  
         $.ajax({
            url: 'src/ldf_dpd_list.php?search-n=urlplan',
            success: function (data) {
               $('#dpd').html(data);
            }
         });
      });
   });

Hi I am new to javascript and Ajax and trying to pass the variable urlplan, what is the correct way to pass the variable.


It looks like the following should do it:

var urlparts = urlquery.split('='); 
var urlplan  = urlparts[1];

// ...

$.ajax({
   url: 'src/ldf_dpd_list.php?search-n=' + urlplan,
   success: function (data) {
      // ...
   }
});


url: "src/ldf_dpd_list.php?search-n="+urlplan,

for more than one variable

url: "src/ldf_dpd_list.php?search-n="+urlplan+"&amp;xyz="+variablName,


This is a snippet from my easyXDM library

var _query = (function(){
    var query = {}, pair, search = location.search.substring(1).split("&"), i = search.length;
    while (i--) {
        pair = search[i].split("=");
        query[pair[0]] = pair[1];
    }
    return query;
}());

Use it like this

alert(_query["urlplan"]);

or

alert(_query.urlplan);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号