javascript event.preventdefault() 在 jquery.click with Rails

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

本文关键字:jquery click with Rails event preventdefault javascript      更新时间:2023-09-26

我有一个 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,而不是类.submit