从链接获取javascript中的变量
Fetch variable in javascript from a link
我正试图通过javascript发送一个PHP变量,但遇到了问题。我使用的是我在网上找到的jQuery弹出窗口,当用户点击一个项目时,我需要在我想添加表单的弹出窗口中获取item_id变量。
我的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 value
和document.getElementById('popup1').innerHtml = itemID;
使用复杂的函数检索URL查询参数:
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, " "));
}
[从此处采用]
简单地这样称呼它:
getParameterByName('item_id', someLink);
工作Fiddle:http://jsfiddle.net/ADpYT/
您在原始代码中有一个简单的疏忽。。当您获得dim
变量时,您会得到一个数组,其中包含在&处拆分的2个项。在上面的代码中,你得到了["w=500", "item_id=some_value"]
。所以为了得到itemID,你必须使用dim[1]。var itemID=dim[1].split('=')[1];
相关文章:
- 调用类向后变量 (JavaScript)
- 初始化父类中的变量(JavaScript/CoffeeScript 习语)
- 可以'找不到变量javascript错误
- 输出数组变量javascript
- 自提交表单访问变量javascript
- 将变量 javascript 添加到用于旋转图像的链接中
- 在文本字段中显示保存的本地存储变量 - javascript
- 将变量添加到变量 JavaScript 中
- 获取要在变量 JavaScript 中使用的用户输入值
- 如何在变量 Javascript 中删除双引号
- 使用变量 javascript 从 json 文件中获取数据
- 读取输入和打印变量 - JavaScript 和 HTML 4.01
- 传递 PHP 变量 JavaScript 窗口位置
- 重置变量 JavaScript
- 变量 JavaScript 中的变量
- 尝试使用多个函数时无法使用全局变量 - JavaScript - 初学者
- 如何按值将数组分配给另一个变量 JavaScript
- setTimout 搞砸了变量 JavaScript
- 用内部函数变量更改外部函数变量?Javascript
- 根据变量 JavaScript 增加