意外的令牌)错误JS代码找不到错误?

时间:2013-04-09 22:27:10

标签: javascript jquery

我在以下代码中一直收到错误意外令牌“)。谁能告诉我我的代码有什么问题?

function postFrom()
{
 /* contactform */
$.post('/contact_owner/send/', { 

customer_name: $('[name="customer_name"]').val(),
customer_phone: $('[name="customer_phone"]').val(),
customer_email: $('[name="customer_email"]').val(),

customer_message: $('[name="customer_message"]').val() +' \n-Adress: '+ $('[name="customer_address"]').val() +' -Postnummer: '+ $('[name="customer_zipcode"]').val() +' -Ort: '+ $('[name="customer_city"]').val() + ' -Markisvävar: '+ final,

subject:$('[name="customer_name"]').val() +' - '+ $('[name="sub"]').val()  },     function(data){
  if (data) {
    $('#status_notice').css('color', 'red');
    $('#status_notice').html(data);
  } else {
    $('#status_notice').css('color', 'green');
    $('#status_notice').html('Ditt meddelande skickades.');
    $('[name="customer_name"]').val('');
    $('[name="customer_phone"]').val('');
    $('[name="customer_email"]').val('');
    $('[name="customer_address"]').val('');
    $('[name="customer_zipcode"]').val('');
    $('[name="customer_city"]').val('');
    $('[name="customer_message"]').val('');
  $('input[type=checkbox]').each(function() 
  { 
    this.checked = false; 
  }); 

}
});
event.preventDefault();
});
});                                                       

/* contactform END */

}//end function

4 个答案:

答案 0 :(得分:5)

您正在尝试使用}):来关闭该功能(您只需要使用}),而且您还有一些错误的右括号。

试试这个:

function postFrom()
{
 /* contactform */
$.post('/contact_owner/send/', { 

  customer_name: $('[name="customer_name"]').val(),
  customer_phone: $('[name="customer_phone"]').val(),
  customer_email: $('[name="customer_email"]').val(),

  customer_message: $('[name="customer_message"]').val() +' \n-Adress: '+ $('[name="customer_address"]').val() +' -Postnummer: '+ $('[name="customer_zipcode"]').val() +' -Ort: '+ $('[name="customer_city"]').val() + ' -Markisvävar: '+ final,

  subject:$('[name="customer_name"]').val() +' - '+ $('[name="sub"]').val()  },     function(data){
  if (data) {
    $('#status_notice').css('color', 'red');
    $('#status_notice').html(data);
  } else {
    $('#status_notice').css('color', 'green');
    $('#status_notice').html('Ditt meddelande skickades.');
    $('[name="customer_name"]').val('');
    $('[name="customer_phone"]').val('');
    $('[name="customer_email"]').val('');
    $('[name="customer_address"]').val('');
    $('[name="customer_zipcode"]').val('');
    $('[name="customer_city"]').val('');
    $('[name="customer_message"]').val('');
    $('input[type=checkbox]').each(function() 
  { 
    this.checked = false; 
}); 

}
});
event.preventDefault();
}

答案 1 :(得分:4)

格式化代码后,最后两行});行似乎无关紧要,应该删除。

格式化后:

function postFrom()
{
    /* contactform */
    $.post('/contact_owner/send/', {
        customer_name: $('[name="customer_name"]').val(),
        customer_phone: $('[name="customer_phone"]').val(),
        customer_email: $('[name="customer_email"]').val(),
        customer_message: $('[name="customer_message"]').val() + ' \n-Adress: ' + $('[name="customer_address"]').val() + ' -Postnummer: ' + $('[name="customer_zipcode"]').val() + ' -Ort: ' + $('[name="customer_city"]').val() + ' -Markisvävar: ' + final,
        subject: $('[name="customer_name"]').val() + ' - ' + $('[name="sub"]').val()}, function(data) {
        if (data) {
            $('#status_notice').css('color', 'red');
            $('#status_notice').html(data);
        } else {
            $('#status_notice').css('color', 'green');
            $('#status_notice').html('Ditt meddelande skickades.');
            $('[name="customer_name"]').val('');
            $('[name="customer_phone"]').val('');
            $('[name="customer_email"]').val('');
            $('[name="customer_address"]').val('');
            $('[name="customer_zipcode"]').val('');
            $('[name="customer_city"]').val('');
            $('[name="customer_message"]').val('');
            $('input[type=checkbox]').each(function()
            {
                this.checked = false;
            });
        }
    });
    event.preventDefault();
    /* contactform END */

}

答案 2 :(得分:3)

错字错误,额外括号

});
event.preventDefault();
}); // delete it
}); // delete it

答案 3 :(得分:2)

更好的缩进可能会对你有很大的帮助,但我认为这两者都是});假设event.PreventDefault应该在函数的末尾,则不需要行(33,34)。