文本框使用 if else 语句打开我的链接 JavaScript

textbox opening my link using if else statement javascript

本文关键字:我的 链接 JavaScript 语句 else if 文本      更新时间:2023-09-26

我想要完全相同的东西,但否则它将打开我另一个.htm文件的链接。 整个代码很好,但是当我在textfeld中输入某些内容时,我希望在警报或主窗口中出现一个链接。

<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>​​​

但在其他情况下,它将打开我另一个.htm文件的链接。

"否则"的意思是在表单提交返回 true 时打开链接?如果是这样,只需将链接放在表单的 action 属性中,而不是 #

但是当我在文本中输入某些内容时,我希望出现一个链接 在警报中或在主窗口中。

如果要显示显示链接的消息,请在// If text_name is not null continue processing后加上alert('www.example.com');


编辑

我调整了你的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>