[英]Retrieve variable value - non input
I have a PHP File with something like this: 我有一个这样的PHP文件:
<script type="text/javascript">
var page = '1';
</script>
<select class='search' name="type" ID="type">
<option value='%'>All Types</option>
.....
</select>
I know how to retrieve the value of the type with JQuery. 我知道如何使用JQuery检索该类型的值。
Is there a way (besides a hidden field) to retrieve the value of the JavaScript variable page
? 有没有办法(除了隐藏字段)检索JavaScript变量
page
的值? I have tried this but keep getting undefined: 我试过这个但是一直未定义:
var page = $("#" + this.page).val();
Your variable page
is global throughout the scope of your document. 您的变量
page
在整个文档范围内都是全局的。 You can access simply by 你可以简单地访问
var myPage = page;
as long as it was set in the document before this script was ran. 只要在运行此脚本之前在文档中设置它。
试试简单:
var page = $("#" + page).val();
A better implementation, not knowing the real purpose, would be using data attributes. 更好的实现,不知道真正的目的,将使用数据属性。
So your html would include <body data-page="1">
(you can add it to any element). 所以你的html将包含
<body data-page="1">
(你可以将它添加到任何元素)。
And your JS would do var page = $('body').data('page'); //page = 1
你的JS会做
var page = $('body').data('page'); //page = 1
var page = $('body').data('page'); //page = 1
http://html5doctor.com/html5-custom-data-attributes/ http://html5doctor.com/html5-custom-data-attributes/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.