javascript event.preventdefault() 无法使用 Rails 在 jquery.click 上工作

javascript event.preventdefault() not working on jquery.click with Rails

我有一个 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

});

为什么它不起作用?

改变,来自

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

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

元素的 ID 为 #submit,而不是 class .submit