-2

I've been getting some errors with my javascrript and i can't figure out what is wrong. I just made a simple jQuery statement to check if an email field had a default value and if so, clear the field

Here is the jsFiddle link to the code i am working with: http://jsfiddle.net/qLfBH/ I can't figure out why the jslint is complaining about the line 5 "missing semicolon" and such.

$(document).ready(function () {
    var email_default = "Enter your email address...";

    $(':input').filter('[type="email"]').attr('val', email_default).focus({
    if ($(this).val() == email_default) ;{
        $(this).attr('val', ' ');
    }
    });
});

I have also tried using firebug to debug, but the javascript would not show in the scripts panel- probably because of some syntax error. Any help would be much appreciated.

UPDATE

I edited the code but its still complaining about a "missing semicolon" (see link to fiddle http://jsfiddle.net/qLfBH/3/)

$(document).ready(function () {
var email_default = "Enter your email address...";

$(':input').filter('[type="email"]').attr('value', email_default).focus({
    if ($(this).val() == email_default) {
        $(this).attr('val', ' ');
    }

});
});

FIXED

Ok thanks for the help everyone. I forgot the "function" under the focus. I corrected the errors below and its working :)

$(document).ready(function () {
var email_default = "Enter your email address...";

$(':input').filter('[type="email"]').attr('value', email_default).focus(function () {
    if ($(this).val() == email_default) {
        $(this).attr('val', ' ');
    }
});
});
4

4 に答える 4

1

いくつかの問題。クリーンアップされたバージョンは次のとおりです。

var email_default = "Enter your email address...";
$(':input[type="email"]').val(email_default).on('focus', function () {
    if ($(this).val() == email_default) {
        $(this).val(' ');
    }
});

jsFiddle の例

jQuery を使用せずに、HTML5 のプレースホルダー属性を次のように使用することもできます。

<input type="email" placeholder="Enter your email address...">
于 2013-03-19T16:23:53.620 に答える
1

if ステートメントの後にセミコロンを削除します

から

if ($(this).val() == email_default) ;{ // <----SHOULDNT BE THERE
    $(this).attr('val', ' ');
}

if ($(this).val() == email_default) {
    $(this).attr('val', ' ');
}
于 2013-03-19T16:20:29.733 に答える
0

JavaScript コードをこれに置き換えます

$(document).ready(function () {
  var email_default = "Enter your email address...";

  $('input').filter('[type=email]').val(email_default).focus(function() { 
        if ($(this).val() == email_default) {
            $(this).val('');
    }
    });
});
于 2013-03-19T16:33:59.237 に答える
0

更新を参照してください:

$(document).ready(function () {
  var email_default = "Enter your email address...";

  $('input[type="email"]').val(email_default).focus(function () {  //<-- add function()
    if ($(this).val() == email_default) {   //<-- removed semicolon here
        $(this).val(' ');                   //<-- either .attr('value',' ') or .val(' ')
    }
  });
});

サンプル

于 2013-03-19T16:20:59.593 に答える