-1

私はまったく同じことをしたいのですが、それ以外の場合は、他の .htm ファイルのリンクが開きます。コード全体は問題ありませんが、テキストフィールドに何かを入力すると、アラートまたはメインウィンドウにリンクが表示されます。

<script Language="JavaScript">
<!--
    function Blank_TextField_Validator() {
        // Check the value of the element named text_name from the form named text_form
        if (text_form.text_name.value == "") {
            // If null display and alert box
            alert("Please fill in the text field.");
            // Place the cursor on the field for revision
            text_form.text_name.focus();
            // return false to stop further processing
            return (false);
        }
        // If text_name is not null continue processing
        return (true);
    }
-->
</script>
<form name="text_form" method="get" action="#" onsubmit="return Blank_TextField_Validator()">
    <input type="text" name="text_name" >
    <input type="submit" value="Submit">
</form>​​​
4

1 に答える 1

0

それ以外の場合は、他の .htm ファイルのリンクが開きます。

「他の場合」は、フォーム送信がtrueを返したときにリンクを開くことを意味しますか? actionその場合は、代わりにフォームの属性にリンクを入れてください#

しかし、テキストフィールドに何かを入力すると、アラートまたはメインウィンドウにリンクが表示されます。

リンクを示すメッセージを表示したい場合はalert('www.example.com');// If text_name is not null continue processing.


編集

私はあなたのJS関数を適応させました:

<script Language="JavaScript">
<!--
function Blank_TextField_Validator() {
    // Check the value of the element named text_name from the form named text_form
    if (text_form.text_name.value == "") {
        // If null display and alert box
        alert("Please fill in the text field.");
        // Place the cursor on the field for revision
        text_form.text_name.focus();
        // return false to stop further processing
        return (false);
    }
    // If text_name is not null continue processing
    if (text_form.text_name.value == "project")
        window.open('project.htm');
    else if (text_form.text_name.value == "under")
        window.open('underconstruction.htm');
    else
        alert("Invalid keyword!");
    return (true);
}
-->
</script>
于 2012-05-02T05:47:58.510 に答える