0

トリッキーなウィキ検索の問題を解決するために、Javascript ソリューションを PHP ファイルに実装しました。

残念ながら、私には 2 つの問題があります。1 つは、関数が呼び出されていないことです。もう 1 つは、エラー コンソールに解決できない構文エラーが表示されることです。まず、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 がエラー コンソールで定義されていないことです。

2 番目の問題は、構文エラーです。生成された 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="&quot;Applies to=Platform 1.0&quot;" id="p1" />&nbsp;<label for="">Platform 1.0</label><input type="checkbox" name="2" value="&quot;Applies to=Platform 2.0&quot;" id="p2" />&nbsp;<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 .= '&nbsp;' . 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 .= '&nbsp;' . 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;
}
4

2 に答える 2

0

私は、すべてを1行の間に配置することで、</p>問題をなんとか取り除くことができました。これは、ファイルをLinuxサーバーに配置するときに、エディター(Windowsの場合はNotepad ++)に問題があるのではないかと思います。しかし、私はNotepad ++でそれを処理できると思いました(PHPでは問題ありません)。<script></script>

スクリプトが呼び出されなかったという事実は、実際には構文エラーが原因でした。これは、別の質問の手がかりからわかったものです。'search''searchbox'

それが解決されると、関数はエラーなしで呼び出されました。

他の誰かがそれを望む場合のために、ここに完全な動作コードがあります:

    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('searchboxInput').value;if (platform1 * platform2 >0) text = text + ' "Applies to=Platform 1.0" "Applies to=Platform 2.0"';else if (platform1) text = text + ' "Applies to=Platform 1.0"';else if (platform2) text = text + ' "Applies to=Platform 2.0"';document.getElementById('searchboxInput').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',
                'id' => 'searchboxInput',
                'type' => 'text',
                'value' => $this->mDefaultText,
                'size' => $this->mWidth,
            )
        );

        $htmlOut .= $this->mBR;

        // Checkbox
        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'checkbox',
                'name' => '1',
                'id' => 'p1'
            )
        );
        // Label
        $htmlOut .= '&nbsp;' . Xml::label( 'Platform 1.0' );

        // Checkbox
        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'checkbox',
                'name' => '2',
                'id' => 'p2'
            )
        );
        // Label
        $htmlOut .= '&nbsp;' . Xml::label( 'Platform 2.0' );

        // Line break
        $htmlOut .= $this->mBR;

        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'submit',
                'name' => 'fulltext',
                'class' => 'searchboxSearchButton',
                'value' => 'search',
                '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;
    }   
于 2012-05-23T06:43:11.133 に答える
0

要素<p>内のタグの位置が間違っている問題を修正します。<script>同じブロックで宣言されているため、これらのタグ内の構文エラー<script>が関数の登録を妨げている可能性があります。

UPDATErender()したがって、最終的にはWikiエンジンがおそらく呼び出して いるため、言うのは難しいInputBox. しかし、ここで役立つかもしれない提案があります。ほとんどの要素では、Xmlクラスの静的メソッドを使用して要素を追加しています。scriptでタグを作成してみましたXml::openElementか? 次に、これらの呼び出しの間にスクリプト本体のヒアドキュメント宣言を配置できます。Wiki側で何かがp勝手にタグを挿入している場合、この方法を使えばそうならないように出力できるのではないでしょうか。正直なところ、投稿したコードには、ここにコードから段落タグを挿入する必要があることを示すものは何も表示されないため、試してみる価値があります。

于 2012-05-21T14:03:19.473 に答える