运行 Javascript onFocus - 更改 DIV html 文本

发布于 2024-12-02 16:51:49 字数 465 浏览 0 评论 0原文

目前,我正在尝试设置我的联系表单,以便在输入无效电子邮件时给出错误消息。当提交空白或不正确的电子邮件时,页面应执行以下操作:

  • 注册文本更改为“重试”
  • 输入字段中出现红色文本,说明“输入的地址无效”

当用户将焦点放在输入字段上,或按“重试';所有表单元素都应返回到其原始状态。我已经能够使用 onFocus 方法将文本从红色变为黑色。我尝试创建一个名为 Reset 的小型 JavaScript,我希望将 DIV 文本从“重试”更改回“注册”。

我确信我的问题与 Javascript 有关。有人能指出我正确的方向吗? :)

代码参考: http://itsmontoya.com/work/playdeadcult/indextest.php

Currently I am trying to setup my contact form to give an error message when an invalid email is entered. When a blank or incorrect e-mail is submitted the page should do the following:

  • Sign Up text changes to 'Retry'
  • Red text appears within input field stating 'Address entered not valid'

When the user focuses on the input field, or presses 'retry'; all the form elements should go back to their original state. I have been able to get the text to change from red to black using the onFocus method. I attempted to create a small little javascript named reset, which I hoped to change the DIV text from 'Retry' back to 'Sign Up'.

I'm certain my issue is with the Javascript. Can someone point me in the right direction? :)

For code reference:
http://itsmontoya.com/work/playdeadcult/indextest.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生一梦 2024-12-09 16:51:49

一种方法是使用 html:

<form action="" method="post">
    <fieldset>
        <label for="emailAddress">Sign up for the newsletter</label>
        <input type="text" id="emailAddress" name="email" />
        <button id="submitButton" type="submit">Submit</button>
    </fieldset>
</form>

和 JavaScript:

var emailEntry = document.getElementById('emailAddress');
var button = document.getElementById('submitButton');
var defaultButtonText = button.innerHTML;

emailEntry.onblur = function(){
    var val = this.value;

    // the following IS NOT a valid test of email address validity
    // it's for demo purposes ONLY

    if (val.length < 5 && val.indexOf('@') == -1){
        button.innerHTML = 'retry';
        button.style.color = 'red';
    }
    else {
        button.innerHTML = defaultButtonText;
        button.style.color = 'black';
    }
};

button.onclick = function(){
    // prevent submission of the form if the email was found invalid
    // and the innerHTML was therefore set to 'retry'
    if (this.innerHTML == 'retry'){
        return false;
    }
};

JS Fiddle demo

One way of doing this is, with the html:

<form action="" method="post">
    <fieldset>
        <label for="emailAddress">Sign up for the newsletter</label>
        <input type="text" id="emailAddress" name="email" />
        <button id="submitButton" type="submit">Submit</button>
    </fieldset>
</form>

And the JavaScript:

var emailEntry = document.getElementById('emailAddress');
var button = document.getElementById('submitButton');
var defaultButtonText = button.innerHTML;

emailEntry.onblur = function(){
    var val = this.value;

    // the following IS NOT a valid test of email address validity
    // it's for demo purposes ONLY

    if (val.length < 5 && val.indexOf('@') == -1){
        button.innerHTML = 'retry';
        button.style.color = 'red';
    }
    else {
        button.innerHTML = defaultButtonText;
        button.style.color = 'black';
    }
};

button.onclick = function(){
    // prevent submission of the form if the email was found invalid
    // and the innerHTML was therefore set to 'retry'
    if (this.innerHTML == 'retry'){
        return false;
    }
};

JS Fiddle demo.

や莫失莫忘 2024-12-09 16:51:49

试试这个:

function validate() {
    if($("#buttonDiv").html()== 'Retry')
    {
        // oops! They are trying to get back to where they were by 
        // re-clicking retry. Let's reset this thing
        reset()
        return false;
    }
    var emDiv = document.getElementById('email') // only look up once.
    if(emDiv) {
        // the space after the . should allow for 6 long
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
        var address = emDiv.value;
        // good form issue -- never use == false. use !
        if(!reg.test(address)) {
            emDiv.value = 'Address entered not valid';
            emDiv.style.color = '#bf4343';
            emDiv.style.fontSize = '12px';
            $("#buttonDiv").html( 'Retry' );
            return false;
        }
    }
}

Try this instead:

function validate() {
    if($("#buttonDiv").html()== 'Retry')
    {
        // oops! They are trying to get back to where they were by 
        // re-clicking retry. Let's reset this thing
        reset()
        return false;
    }
    var emDiv = document.getElementById('email') // only look up once.
    if(emDiv) {
        // the space after the . should allow for 6 long
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
        var address = emDiv.value;
        // good form issue -- never use == false. use !
        if(!reg.test(address)) {
            emDiv.value = 'Address entered not valid';
            emDiv.style.color = '#bf4343';
            emDiv.style.fontSize = '12px';
            $("#buttonDiv").html( 'Retry' );
            return false;
        }
    }
}
鹿港巷口少年归 2024-12-09 16:51:49

这只是您需要帮助的 Reset() 函数的注释掉块吗?如果是这样,您可以尝试它的 jQuery 版本,因为无论如何您都加载了 jQuery =)

以下是您的 Reset() 函数,它将文本颜色更改为红色:

$('#email').val('').css('color', '#FF0000').css('font-size', '18px');

以下是您的 Blur() 函数,它将文本颜色更改为黑色。将此附加到您的电子邮件输入文本框,您应该进行设置:

$('#email').blur().css('color', '#000000');

Is it just the commented out block of your reset() function what you need help with? If so, you can try the jQuery version of it since you have jQuery loaded anyway =)

The following is for your reset() function which changes text color to red:

$('#email').val('').css('color', '#FF0000').css('font-size', '18px');

The following is for your blur() function which changes text color to black. Attach this to your email input text box as well and you should be set:

$('#email').blur().css('color', '#000000');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文