当Chrome试图在页面上加载脚本文件时,收到主题错误.它说它在javascript文件的最后一行.我似乎没发现有什么问题.firefox中没有错误,脚本按预期工作.只是使用表单验证

// JavaScript Document
$(function() {
  $('#wm-form').submit(function() {
    var errors = false;
    var errorMsg = "";
    $('.required').each(function() {
      if(!validField($(this))) {
        errorMsg += $(this).attr('name').capitalize() + " cannot be blank\n";
        errors = true;
      }
    });
    var emailAddress = $('#email');
    if(isValid(emailAddress) && !(/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/.test(emailAddress.val()))) {
      errorMsg += "Not a valid email address. Please enter in a correctly formatted email address";
      errors = true;
    }
    if(errors) {
      alert(errorMsg);
      return false;
    }
  });

  $('.form-focus').click(function() {
    $(document).scrollTop(0);
    $('#first_name').focus();
    return false;
  });
});

function validField(element) {
  if(!isValid(element.val()) || (element.attr('placeholder') && element.attr('placeholder') == element.val()) || 
    (element.attr('type') == 'radio' && !checkedRadio(element))) {
    return false;
  }
  else {
    return true;
  }
}

function isValid(ele) {
  if(ele == null || ele == '') {
    return false;
  }
  else {
    return true;
  }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

function checkedRadio (element) {
  var valid = false;
  $('input[name="'+ element.attr("name") +'"]:checked').each(function() {
    valid = true;
  });

  return valid;
}​

推荐答案

There's some sort of bogus character at the end of that source. Try deleting the last line and adding it back.

I can't figure out exactly what's there, yet ...

edit — I think it's a zero-width space, Unicode 200B. Seems pretty weird and I can't be sure of course that it's not a Stackoverflow artifact, but when I copy/paste that last function including the complete last line into the Chrome console, I get your error.

A notorious source of such characters are websites like jsfiddle. I'm not saying that there's anything wrong with them — it's just a side-effect of something, maybe the use of content-editable input widgets.

如果您怀疑自己得了这种病,并且您使用的是MacOS或Linux/Unix,od命令行工具可以向您显示源代码文件字符中的数字值(尽管方式相当难看).一些IDE和编辑器也可以显示"有趣"的字符.请注意,这样的字符always不是问题.例如,在字符串常量中嵌入Unicode字符是完全可以的(至少在大多数合理的编程语言中是这样).当语言解析器遇到不需要的字符时,问题就开始发生了.

Jquery相关问答推荐

使用动态类名的gm_addStyle?

通过 jQuery 提取 application/json 数据

用 jQuery/JavaScript 检测数字或字母?

哪个 JQuery Select 器会排除与给定 Select 器匹配的父项的项目?

jQuery - 向下滚动时缩小的粘性标题

在 select2 中使用 AJAX 进行标记

使用 jQuery 和 HTML 导出为 CSV

如何计算具有相同类的元素数量?

将输入更改为大写

jQuery.parseJSON 与 JSON.parse

如何在 JQuery DataTable 中默认显示所有行

验证外部脚本是否已加载

将字符串true和false转为布尔值

动态创建并提交表单

jQuery Select 器中前导冒号的目的是什么?

JQuery:如果 div 可见

检测 jQuery UI 对话框是否打开

CORS - 如何预检httprequest?

D3 和​​ jQuery 有什么区别?

如何在 jquery 中获取 textarea 的值?