提问人:Brandon S. 提问时间:4/7/2016 最后编辑:SammitchBrandon S. 更新时间:4/7/2016 访问量:320
我是怎么搞砸我的 Javascript 的?
How did I mess up my Javascript?
问:
我把我的代码搞砸得很厉害,所以我需要一些帮助。
我收到以下错误:
SyntaxError:预期的表达式,已获得脚本末尾
这个错误似乎告诉我,我的大括号或分号不合适,而且由于今天的疯狂性质,我似乎找不到它。我正在尝试通过将事件侦听器功能移动到 UserInterface.js 中的函数处理程序下来使我的 Javascript 不那么突兀;但是,我需要在PHP和onload事件之间传递一个“Fields”数组,所以我想将onload事件保留在:中,除非有人可以告诉我传递此变量的更好方法。window.onload
<body onload="OnLoad('.$EncodedFields . ');">';
不知何故,在我的干预中,我触发了这个错误。有谁知道如何解决这个错误?我应该如何正确地将 php 变量传递给 Javascript 以在 onload 事件中使用?
用户界面.php
class UserInterface {
var $ParentAppInstance;
function __construct($AppInstance){
$this->ParentAppInstance = $AppInstance;
$this->DrawPageHTML();
$this->DrawDBSetDropdown();
$this->DrawQueryForm();
}
//Override thjis function to change the HTML and PHP of the UI page.
protected function DrawPageHTML(){
$DBSet_Num = filter_var($this->ParentAppInstance->CurrentDBSet_Str, FILTER_SANITIZE_NUMBER_INT);
$CurrDBSet_Obj = $this->ParentAppInstance->DBSets_Arr[$DBSet_Num];
$EncodedFields = json_encode($CurrDBSet_Obj->GetDBSetFields());
echo '<body onload="OnLoad('. $EncodedFields .');">';
echo '
<div id="DebugOutput"></div>
</body>
';
//json_encode($CurrDBSet_Obj->GetDBSetFields())
echo '$AppInstanceData: ' . '<br>';
echo '--CurrentDBSet_Str: ' . $this->ParentAppInstance->CurrentDBSet_Str;
}
protected function DrawDBSetDropdown(){
echo '<div align="right">';
echo '<select onchange="SwitchDatabaseSet();" name="DBSetList" form="DBSetSelector" id="DBSetSelector">';
$i = 0;
foreach ($this->ParentAppInstance->DBSets_Arr as $DBSet){
if ($DBSet->DBSetName == $this->ParentAppInstance->CurrentDBSet_Str){
echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
}
}
foreach ($this->ParentAppInstance->DBSets_Arr as $DBSet){
if ($DBSet->DBSetName == $this->ParentAppInstance->CurrentDBSet_Str){/* DO NOTHING. IE. IGNORE IT*/}
else if ($DBSet->DBSetName == 'DBSet0'){/* DO NOTHING. IE. IGNORE IT*/}
else{
//Add the DBSet to the dropdown list.
$i++;
echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
}
}
echo '</select>';
echo '</div>';
}
protected function DrawQueryForm(){
echo '<form action="search.php" method="post">';
echo '<div id="QFormBody">';
$NumActiveQBoxes = $this->ParentAppInstance->Config['ApplicationSettings']['NumberDefaultQueryOptions'];
for ($i = 0; $i < $NumActiveQBoxes; $i++){
echo '<div class="QueryBox" name="QBox_' . $i . '">';
echo '<select name=Field_' . $i . '">';
$DBSet_Num = filter_var($this->ParentAppInstance->CurrentDBSet_Str, FILTER_SANITIZE_NUMBER_INT);
$CurrDBSet_Obj = $this->ParentAppInstance->DBSets_Arr[$DBSet_Num];
foreach($CurrDBSet_Obj->GetDBSetFields() as $Field){
echo '<option>' . $Field . '</option>';
}
echo '</select>';
echo '<input type="text"></input>';
echo '<button class= "RMButton" type="button">-</button>';
echo '</div>';
}
echo '<button type="button" id="add" onclick="AddQueryBox();">+</button>';
echo '<button type="submit" id="submit">SEARCH</button>';
echo '</Form>';
$EncodedFields = json_encode($CurrDBSet_Obj->GetDBSetFields());
echo '<script src=/GLS_DBSearchProject/JavaScript/UserInterface.js></script>';
}
}
用户界面.js
var DBSetFields = [];
var NumQBoxes = 3;
function OnLoad(Fields){
console.log("Alpha");
console.log(Fields);
CloneDBSetFields(Fields);
var RMNodeList = document.getElementsByClassName('RMButton');
for (var i = 0; i < RMNodeList.length; ++i) {
console.log(RMNodeList[i]);
RMNodeList[i].onclick = RemoveQBox; // Calling myNodeList.item(i) isn't necessary in JavaScript
}
}
function Fields_FOREACH(ELEMENT, INDEX, ARRAY){
var FieldOption = document.createElement('option');
FieldOption.setAttribute('value', ARRAY[INDEX]);
FieldOption.innerHTML = ARRAY[INDEX];
this.appendChild(FieldOption);
}
function CloneDBSetFields(Fields){
console.log("CloneDBSetFields");
DBSetFields = Fields;
}
function AddQueryBox(){
NumQBoxes += 1;
var NewQBox = document.createElement('div');
NewQBox.setAttribute('class', 'QueryBox');
//Create and fill Field Selector dropdown "select" element
var FieldSelector = document.createElement('select');
FieldSelector.setAttribute('name', 'Field_' + NumQBoxes);
//foreach element in Fields
console.log(DBSetFields);
DBSetFields.forEach(Fields_FOREACH, FieldSelector);
//Create and fill
var QueryText = document.createElement('input');
QueryText.setAttribute('type', 'text');
QueryText.setAttribute('name', 'Query_' + NumQBoxes);
//Create "-" Remove button for removing query lines.
var RemoveButton = document.createElement('button');
RemoveButton.innerHTML = "-";
RemoveButton.setAttribute('type', 'button');
RemoveButton.setAttribute('class', 'RMButton');
RemoveButton.addEventListener("click", RemoveQBox);
//Combine the individual elements into a new query box and insert the new query box into the HTML Document
NewQBox.appendChild(FieldSelector);
NewQBox.appendChild(QueryText);
NewQBox.appendChild(RemoveButton);
document.getElementById("QFormBody").insertBefore(NewQBox, document.getElementById("add"));
}
function RemoveQBox(e){
console.log("Remove");
var RemoveButton = this; //this == e.currentTarget
console.log(RemoveButton);
var QBox = RemoveButton.parentNode;
QBox.remove();
NumQBoxes -= 1;
}
答:
1赞
Brandon S.
4/7/2016
#1
所以显然:
正文 onload=“OnLoad('. $EncodedFields .');”
是导致问题的实际原因。出于某种原因,我还没有完全弄清楚为什么在这种情况下 php 连接运算符不起作用。
将此行更改为或交换单引号和双引号(同时删除句点)也解决了此问题。body onload="OnLoad{$EncodedFields};"
评论
JavaScript,请使用 jslint
来检查你的代码是否有效。