javascript event.preventdefault()没有使用Rails在jquery.click上工作

时间:2015-01-29 13:05:17

标签: javascript jquery ajax forms

我有一个Rails表单。但我不想通过Rails提交表单。我想通过Ajax提交它。

在视图文件

          <li><%= submit_tag 'Save', id: 'submit', :class => 'button' %></li>

在js文件中

$(".submit").click(function() {
    alert("hi");
    event.preventDefault();

    health_marks_array = form_hash_and_submit();
    if (0 < health_marks_array.length) {
        call_form_submit(health_marks_array);
    }
    else{
        location.href = '/thinking_health_card/health_marks/new?form_name=' + form_name + '&student_id=' + student_id + '&health_exam_id=' + health_exam_id
    }
    return false

});

也试过

$(".submit").click(function(event) {
    alert("hi");
    event.preventDefault();

    health_marks_array = form_hash_and_submit();
    if (0 < health_marks_array.length) {
        call_form_submit(health_marks_array);
    }
    else{
        location.href = '/thinking_health_card/health_marks/new?form_name=' + form_name + '&student_id=' + student_id + '&health_exam_id=' + health_exam_id
    }
    return false

});

为什么它不起作用?

1 个答案:

答案 0 :(得分:3)

改变
$(".submit").click(function() {});

$("#submit").click(function(event) {
   event.preventDefault();
   ....
});

元素的标识为#submit,而非.submit