为了解决棘手的 wiki 搜索问题,我们在 PHP 文件中实现了 Javascript 解决方案。
不幸的是,我有两个问题。一是没有调用该函数,二是我在错误控制台中看到我无法解决的语法错误。首先,这是 PHP 文件中的代码:
$htmlOut .= <<<ENDOFBLOCK
<script language="javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 1.0 ' + text;
if (platform2) text = 'Applies to=Platform 2.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</script>
ENDOFBLOCK
;
所以,第一个问题是我看到appendAndSubmit 没有在错误控制台中定义。
第二个问题是语法错误。生成的 HTML 源代码为:
<p><script language="javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 1.0 ' + text;
if (platform2) text = 'Applies to=Platform 2.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</p>
</script><div align="center" style="background-color:transparent"><form name="searchbox" id="searchbox" class="searchbox" action="/wiki/index.php?title=Special:Search"><input class="searchboxInput" name="search" type="text" value="" size="50" /><br /><input type="checkbox" name="1" value=""Applies to=Platform 1.0"" id="p1" /> <label for="">Platform 1.0</label><input type="checkbox" name="2" value=""Applies to=Platform 2.0"" id="p2" /> <label for="">Platform 2.0</label><br /><input type="submit" name="fulltext" class="searchboxSearchButton" value="Go!" onClick="appendAndSubmit();" /></div></form>
注意</p>
发生在之前</script>
,而<p>
发生在之前<script>
。
谁能告诉我我做错了什么?
appendAndSubmit 函数的调用在这里:
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'searchboxSearchButton',
'value' => 'Go!',
'onClick' => 'appendAndSubmit();'
)
);
完整方法:
public function getSearchPlatform() {
// Use button label fallbacks
global $wgContLang;
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( 'tryexact' );
}
if ( !$this->mSearchButtonLabel ) {
$this->mSearchButtonLabel = wfMsgHtml( 'searchfulltext' );
}
$htmlOut .= <<<ENDOFBLOCK
<script type="text/javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 3.0 ' + text;
if (platform2) text = 'Applies to=Platform 4.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</script>
ENDOFBLOCK
;
// Build HTML
$htmlOut .= Xml::openElement( 'div',
array(
'align' => 'center',
'style' => 'background-color:' . $this->mBGColor
)
);
$htmlOut .= Xml::openElement( 'form',
array(
'name' => 'searchbox',
'id' => 'searchbox',
'class' => 'searchbox',
'action' => SpecialPage::getTitleFor( 'Search' )->escapeLocalUrl(),
)
);
$htmlOut .= Xml::element( 'input',
array(
'class' => 'searchboxInput',
'name' => 'search',
'type' => 'text',
'value' => $this->mDefaultText,
'size' => $this->mWidth,
)
);
$htmlOut .= $this->mBR;
// Checkbox
$htmlOut .= Xml::element( 'input',
array(
'type' => 'checkbox',
'name' => '1',
'value' => '"Applies to=Platform 1.0"',
'id' => 'p1'
)
);
// Label
$htmlOut .= ' ' . Xml::label( 'Platform 2.0' );
// Checkbox
$htmlOut .= Xml::element( 'input',
array(
'type' => 'checkbox',
'name' => '2',
'value' => '"Applies to=Platform 2.0"',
'id' => 'p2'
)
);
// Label
$htmlOut .= ' ' . Xml::label( 'Platform 2.0' );
// Line break
$htmlOut .= $this->mBR;
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'searchboxSearchButton',
'value' => 'Go!',
'onClick' => 'appendAndSubmit();'
)
);
// Hidden fulltext param for IE (bug 17161)
if( $type == 'fulltext' ) {
$htmlOut .= Xml::hidden( 'fulltext', 'Search' );
}
$htmlOut .= Xml::closeElement( 'div' );
$htmlOut .= Xml::closeElement( 'form' );
// Return HTML
return $htmlOut;
}