jQuery syntax showing error

时间:2015-06-30 13:55:48

标签: jquery

 $("#txtName,#txtEmail,#txtComment").blur(function check()
        if( $(this).val()== ""){
            $(this).val()="Please Fill in this Required Field!";
    });

Please am having error with the above code please help me correct it. Thanks

2 个答案:

答案 0 :(得分:0)

Your syntax is not correct ,try this

 $("#txtName,#txtEmail,#txtComment").blur(function check()
    {
        if( $(this).val()== ""){
            $(this).val()="Please Fill in this Required Field!";
    });

2 things you should try is,you can keep a anonymous function here, and use on event.

  $("#txtName,#txtEmail,#txtComment").on('blur',function(){
            if( $(this).val()== ""){
                $(this).val()="Please Fill in this Required Field!";
        });

答案 1 :(得分:0)

错误1:你不能像这样调用检查功能而你错过了if语句的大括号

错误2:实际上你分配的文字值是错误的。

附加文本值,如 .val(text)

$(this).val("Please Fill in this Required Field!");

最后您的代码

$("#txtName,#txtEmail,#txtComment").blur(function() {
     if($.trim($(this).val()) == ""){
      $(this).val('Please Fill in this Required Field!');
      }
 });

$("#txtName,#txtEmail,#txtComment").blur(check);

function check() {
  if($.trim($(this).val()) == ""){
   $(this).val('Please Fill in this Required Field!');
 }
}