如何从onchange事件中创建全局变量,以便我的js页面中的每个函数都可以使用该变量的值?谁能提供一个工作示例代码。
onchange="showTable()"
event from main.php:
<?php
$linkID = @ mysql_connect("localhost", "root", "Newpass123#") or die("Could not connect to MySQL server");
@ mysql_select_db("seatmapping") or die("Could not select database");
$dbname = "seatmapping";
$sql = "SHOW TABLES FROM $dbname where tables_in_seatmapping!='userauth'";
$result = mysql_query($sql);
$tableNames= array();
while ($row = mysql_fetch_row($result)) {
$tableNames[] = $row[0];
}
echo '<select name="tables" id="tables" onchange="showTable(this.value)">';
echo '<option class="placeholder" selected disabled>Layouts</option>';
foreach ($tableNames as $name){
echo '<option value="'. $name.'">'.$name.'</option>';
}
echo '</select>';
?>
onclick="showAvailable()"
event from main.php
<div class="sliderContainer">
<div id="slider">
<a class="ui-slider-handle" href="#">Start</a>
<a class="ui-slider-handle" href="#">End</a>
</div>
<p>
<a id="timer"><span class="slider-time">10:00 AM</span> to <span class="slider-time2">12:00 PM</span></a>
<a href="#" id="checkAvailable" onclick="showAvailable()">Avail</a>
<a href="#" id="resetAvailable" onclick="resetAvailable()">Reset</a></p>
</div>
.js页面中的函数:
var tableName;
function showTable(tableName){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("showLayout").innerHTML=xmlhttp.responseText;
tableName = xmlhttp.responseText;
}
}
xmlhttp.open("GET","seatmap.php?tableName="+tableName,true);
xmlhttp.send();
}
function showAvailable(){
var startShift = $('#slider').slider("values", 0);
var endShift = $('#slider').slider("values", 1);
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("showLayout").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","seatmap.php?startShift="+startShift+"&endShift="+endShift+"&tableName="+tableName,true);
xmlhttp.send();
}
和这里是一个片段seatmap.php,它太长了,但我使用$tableName为showAvailable和showTable。我还在查询中使用它:
<?php
$startSlider=intval($_GET['startShift']);
$endSlider=intval($_GET['endShift']);
$tableName = strval($_GET['tableName']);
$linkID = @ mysql_connect("localhost", "root", "Newpass123#") or die("Could not connect to MySQL server");
@ mysql_select_db("seatmapping") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT rowId,columnId,status,name,seatid,startShift,endShift from $tableName group by seatid order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo $tableName;
我想要的是如何使值从onchange=showTable(this.value)一个全局变量,所以我可以在函数showAvailable中使用它,并将其传递给php页面。任何形式的帮助都是感激的。
在函数showTable()
之前创建变量,例如var global_var;
,然后在showTable()
中赋值,然后可以在showAvailable()
中使用它,但要记住检查它是否为空。
编辑:你的。js现在应该是这样的(你重写了一些变量):
var tableName;
function showTable(localTable){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("showLayout").innerHTML=xmlhttp.responseText;
tableName = localTable;
}
}
xmlhttp.open("GET","seatmap.php?tableName="+localTable,true);
xmlhttp.send();
}
function showAvailable(){
var startShift = $('#slider').slider("values", 0);
var endShift = $('#slider').slider("values", 1);
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("showLayout").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","seatmap.php?startShift="+startShift+"&endShift="+endShift+"&tableName="+tableName,true);
xmlhttp.send();
}
您可以简单地通过向窗口对象添加一个变量来创建一个全局变量,并且它对所有函数都是可见的。
window.globalValue = "some value";
但这不是最好的方法,我们应该将全局变量保持在最小。最佳实践是-在全局中创建一个命名空间将该变量添加到该全局命名空间。
这样你可以保持全局的整洁,代码更容易维护。
window.Namespace = {};
Namespace.varOnChange = "some value";
我想你要找的是-
var myNamespace = {};
function showTable(tableName){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("showLayout").innerHTML=xmlhttp.responseText;
myNamespace.name = xmlhttp.responseText;
}
}
xmlhttp.open("GET","seatmap.php?tableName="+tableName,true);
xmlhttp.send();
}