多个提交按钮只会首先初始化javascript

多个提交按钮只会首先初始化javascript,javascript,html,Javascript,Html,我在表的每一行中重复下面的提交按钮。单击Submit按钮时,Javascript将初始化并提醒()响应。问题是只有第一个按钮按预期工作,后续按钮重定向到foobar.php 提交按钮: <form name="savefilenote" id="savefilenote" method="post" action="forbidden-savefilenote.php?notetype=2">Note: <input id="filenote" name="filenote"

我在表的每一行中重复下面的提交按钮。单击Submit按钮时,Javascript将初始化并提醒()响应。问题是只有第一个按钮按预期工作,后续按钮重定向到foobar.php

提交按钮:

<form name="savefilenote" id="savefilenote" method="post" action="forbidden-savefilenote.php?notetype=2">Note: <input id="filenote" name="filenote" type="text" value="'.$filenote.'"> <input id="filename" name="filename" type="hidden" value="'.$filename.'"> <input type="submit" value="Save"></form>
注意:
Javascript:

<script>
$(function(){
    $('.savefilenote').on('submit', function(e){

        // prevent native form submission here
        e.preventDefault();

        // now do whatever you want here
        $.ajax({
            type: $(this).attr('method'), // <-- get method of form
            url: $(this).attr('action'), // <-- get action of form
            data: $(this).serialize(), // <-- serialize
            beforeSend: function(){
                $('#result').html('');
            },
            success: function(data){
                $('#result').html(data);

      if(data === "0") {
alert("foo");
      }

      if(data === "1") {
alert("bar");
      }    
            }
        });
    });
});
</script>

$(函数(){
$('.savefilenote')。关于('submit',函数(e){
//禁止在此处提交本机表单
e、 预防默认值();
//现在你想怎么做就怎么做
$.ajax({

类型:$(this).attr('method'),//使用类而不是id


$(“#savefilenote”)
只能找到按钮的一个实例,因为和ID适用于特定元素。如果将其更改为
$(“.savefilenote”)
并将同一类应用于所有按钮,则应使用类而不是ID


$(“#savefilenote”)
只能找到按钮的一个实例,因为和ID适用于特定元素。如果将其更改为
$(“.savefilenote”)
并将相同的类应用于所有按钮。id应特定于单个元素。使用相同的id来标识多个标记,最终只会影响正文中的第一个元素。如果要将行为与一组元素关联,则需要使用
id
应特定于单个元素。使用相同的
id
标识多个标记,最终只会影响正文中的第一个元素。如果要将行为与一组元素关联,则需要使用

可能是因为输入的HTML出错了吗?可能需要双重检查y我们的html位。可能是因为您的输入html搞乱了吗?可能会仔细检查您的html位。如果我将$('#savefilenote')。在('submit',函数(e){更改为$('.savefilenote')。在('submit',函数(e){没有一个提交按钮执行javascript。第一个按钮执行。您必须将表单的html更改为
类=“savefilenote”
而不是
id=“savefilenote”
。如果我更改$('#savefilenote')。在('submit',函数(e){到$('.savefilenote')。在('submit',函数(e){中没有一个提交按钮执行javascript。第一个按钮执行。您必须将表单的html更改为
class=“savefilenote”)“
而不是
id=“savefilenote”