简体   繁体   English

在HTML和jQuery中传递变量

[英]Passing variables in HTML & jQuery

I am using the jQuery mobile framework; 我正在使用jQuery移动框架; and using the framework, I have a grid structured as follows: 并使用框架,我有一个网格结构如下:

<div style="text-align: center;" class="ui-grid-b">

                    <div class="ui-block-a">
                        <a href="lengthDat.html"><img src="images/Longtail.jpg" width="200" height="200"></a>
                        <p style="display: inline;">Longtail</p>
                        <a href="longtail_info.html" rel="external" data-inline="true" data-role="button" 
                                data-mini="true"     data-iconpos="notext" data-icon="info">Longtail Info</a>
                    </div>

                    <div class="ui-block-b">
                        <a href="lengthDat.html"><img src="images/Northern_Dusky.jpg" width="200" height="200"></a>
                        <p style="display: inline;">Northern Dusky</p>
                        <a href="dusky_info.html" rel="external" data-inline="true" data-role="button" 
                                data-mini="true"     data-iconpos="notext" data-icon="info">Dusky Info</a>
                    </div>

                    <div class="ui-block-c">
                        <a href="lengthDat.html"><img src="images/Northern_Spring.jpg" width="200" height="200"></a>
                        <p style="display: inline;">Northern Spring</p>
                        <a href="spring_info.html" rel="external" data-inline="true" data-role="button" 
                                data-mini="true"     data-iconpos="notext" data-icon="info">Spring Info</a>
                    </div>

      .... SNIP 2 ROWS ....
</div>

Now, here is my question. 现在,这是我的问题。 I have all of the images link to the same page, but they are different types of salamanders. 我将所有图像链接到同一页面,但它们是不同类型的蝾螈。 I am trying to get which type they click on, so I can use it on the next page (the measurement page). 我试图找到他们点击的类型,所以我可以在下一页(测量页面)上使用它。

Any help? 有帮助吗?

You can give each link a query string and pick it up with either a server-side language (optimal) or javascript. 您可以为每个链接提供一个查询字符串,并使用服务器端语言(最佳)或javascript进行选择。

eg lengthDat.html?type=longtail , lengthDat.html?type=northern+dusky 例如lengthDat.html?type=longtail lengthDat.html?type=northern+duskylengthDat.html?type=northern+dusky

If you want to know how to read the query string parameters using JavaScript, head on over to this SO question: How can I get query string values in JavaScript? 如果您想知道如何使用JavaScript读取查询字符串参数,请继续阅读这个问题: 如何在JavaScript中获取查询字符串值?

That's quite easy, just extract the extension from the img source (if that's what you meant by type and different salamanders) 这很简单,只需从im​​g源中提取扩展名(如果这是你所说的类型和不同的蝾螈)

$('a').on('click', function(ev) {

    ev.preventDefault();

    var ext = $(this).children('img:eq(0)').attr('src').split('.').pop();

    window.location = '?type=' + ext;
});

I would attach a data-type attribute to the image tage and get it using jquery and append to the url as suggested above. 我会将数据类型属性附加到图像tage并使用jquery获取它并按照上面的建议附加到url。

var type = $('a').find('img').attr('data-type'), url = $('a').attr('href'); var type = $('a')。find('img')。attr('data-type'),url = $('a')。attr('href'); $('a').attr('href', href += '?type=' + type); $('a')。attr('href',href + ='?type ='+ type);

Note: You will have to tweak this code to target the particular element clicked and it is good practice to validate the url, handle urls that already have parameters and ones that don't. 注意:您必须调整此代码以定位单击的特定元素,并且最好验证URL,处理已有参数的URL和不具有参数的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM