2013-02-17 95 views
0

我想获得一个PHP变量通过JavaScript发送,我遇到了麻烦。我正在使用一个我在网上找到的jQuery弹出窗口,当用户点击一个项目时,我需要那个item_id变量可以在弹出窗口中获取,我想添加一个窗体。从链接获取JavaScript变量

我的PHP部分。

<a href="#?w=500&item_id='.$stock['id'].'" rel="popup1" class="poplight"> 

和Javascript。

$(document).ready(function(){ 

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() { 
     var popID = $(this).attr('rel'); //Get Popup Name 
     var popURL = $(this).attr('href'); //Get Popup href to define size 

     //Pull Query & Variables from href URL 
     var query= popURL.split('?'); 
     var dim= query[1].split('&'); 
     var popWidth = dim[0].split('=')[1]; //Gets the first query string value 
     var itemID = dim[0].split('=')[2]; //Gets the second query string value 

     //Fade in the Popup and add close button 
     $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="/layout/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>'); 

     //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css 
     var popMargTop = ($('#' + popID).height() + 80)/2; 
     var popMargLeft = ($('#' + popID).width() + 80)/2; 

     document.getElementById('popup1').innerHtml = itemID; 

     //Apply Margin to Popup 
     $('#' + popID).css({ 
      'margin-top' : -popMargTop, 
      'margin-left' : -popMargLeft 
     }); 

     //Fade in Background 
     $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag. 
     $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

     return false; 
    }); 


    //Close Popups and Fade Layer 
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer... 
     $('#fade , .popup_block').fadeOut(function() { 
      $('#fade, a.close').remove(); 
    }); //fade them both out 

     return false; 
    }); 


}); 

我说什么,我想你的工作,但它不会var itemID = dim[0].split('=')[2]; //Gets the second query string valuedocument.getElementById('popup1').innerHtml = itemID;

+0

究竟什么是行不通的?你看过你的调试器吗?难道,'#popup1'不存在吗? – MCL 2013-02-17 18:58:45

回答

0

使用一个复杂的函数来获取网址查询参数:

function getParameterByName(name, locationObject) 
{ 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(locationObject.search); 
    if(results == null) 
    return ""; 
    else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

[从 here采纳]

简单地说就像这样:

getParameterByName('item_id', someLink); 

工作小提琴:http://jsfiddle.net/ADpYT/

+0

好的,我把它扔到我的函数文件中,并试图在这里调用它。 echo'

'; 但它没有做任何事情。 – user1888260 2013-02-17 18:32:28

+0

@ user1888260看看这个功能,我编辑它。示例呼叫也被编辑。编辑:对不起,你只需要通过链接本身。再次编辑。 – MCL 2013-02-17 18:35:29

+0

好吧,我想我想问什么,我能做些什么来在我的弹出式HTML内以文本格式返回该值? – user1888260 2013-02-17 18:46:37

0

您在最初的代码有一个简单的疏忽.. 当您获得您的dim变量,你有2项的数组拆分为&。在上面的代码中,你得到["w=500", "item_id=some_value"]。所以为了得到itemID你必须使用dim [1]。 var itemID=dim[1].split('=')[1];

+0

啊谢谢,我把它添加到弹出窗口。像这样\t \t $('#popup1')。append(itemID); 但我真的需要它能够在窗体中获取。也许我可以将表单追加到最后? – user1888260 2013-02-17 19:06:01