【问题标题】:Parse error. missing ; before statement. Also doubts about onchange解析错误。失踪 ;声明之前。对onchange也有疑问
【发布时间】:2014-01-03 13:02:14
【问题描述】:

我想比较 jQuery 中的两个字符串,如果它们相同,如果网页中没有错误消息,我会显示提交按钮:

 (function(){
  var tw = window.tw,
    ns = tw.resetpassword = {},
    lib = tw.lib,
    _ = tw.util;

  ns.init = function(){
    $('#password1').checkPassword(){
      var password1 = $('#password1').val()
      console.log(password1);
      var password2 = $('#password2').val()
      console.log(password2);

      if(password1==password2){
        $("#reset-password").removeAttr("style");
        console.log('yes!');
      }else{
        $("#alert-error").removeAttr("style");
      }
    }
    $('#password2').checkPassword(){
      var password1 = $('#password1').val()
      console.log(password1);
      var password2 = $('#password2').val()
      console.log(password2);

      if(password1==password2){
        $("#reset-password").removeAttr("style");
      }else{
        $("#alert-error").removeAttr("style");
      }
    }
  )};
})();

HMTL:

<form action="@securesocial.core.providers.utils.RoutesHelper.handleResetPassword(token).absoluteURL(IdentityProvider.sslEnabled)" autocomplete= "off" method="POST">

        <input class="form-text" id="password1" name="password.password1" type="password" maxlength="128" size="60"  onchange="checkPassword()" placeholder="@Messages("securesocial.passwordChange.newPassword1")">

        <input class="form-text" id="password2" name="password.password2" type="password" maxlength="128" size="60" onchange="checkPassword()" placeholder="@Messages("securesocial.passwordChange.newPassword2")">

        <input class="form-submit email" id="reset-password" type="submit" name="op" value="@Messages("securesocial.password.reset")" style="display:none">
       </form>
    </div>

    <div class="alert alert-error" id="alert-error" font color="red" style="display:none">
    <span style="color: red">@Messages("securesocial.password.error")</span>
    </div>

我收到此错误:

解析错误。失踪 ;声明之前

在这一行

$('#password1').checkPassword(){

我也不知道“onchange”是否正确,有什么想法吗?谢谢!

【问题讨论】:

标签: javascript jquery onchange parse-error


【解决方案1】:

这只是无效的 javascipt 语法!

$('#password1').checkPassword(){
  ...
}

它看起来像是调用方法 (.checkPassword()) 和定义新函数的组合。


查看您的 html,您似乎正在尝试在任一密码字段中的文本更改时调用此函数 checkPassword

<input class="form-text" id="password1" ...  onchange="checkPassword()" ...>

使用 jQuery 时,应避免指定函数内联,而是使用 jQuery .on 方法附加它

 ns.init = function(){
    $('#password1, #password2').on('change',function(){
      var password1 = $('#password1').val()
      console.log(password1);
      var password2 = $('#password2').val()
      console.log(password2);

      if(password1==password2){
        $("#reset-password").removeAttr("style");
      }else{
        $("#alert-error").removeAttr("style");
      }
    });
 }

(注意:无需重复代码两次,上面的代码将相同的行为附加到password1password2。)

您现在也可以从 html 中删除该属性:

<input class="form-text" id="password1" name="password.password1" 
        type="password" maxlength="128" size="60" 
        placeholder="@Messages("securesocial.passwordChange.newPassword1")">

【讨论】:

  • @Anna - 该链接中没有任何内容指定使用 html onchange 属性。根据我更新的答案,他们使用 jQuery 附加了一个事件处理程序!
【解决方案2】:
onchange="checkPassword()"

表示当您的输入字段发生 onchange 事件时,不带参数调用函数 checkPassword

您的 JavaScript 中没有定义 checkPassword 函数,您要么忘记在此处发布它,要么您打算调用 ns.init

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多