【问题标题】:jquery onsubmit doesn't workjquery onsubmit 不起作用
【发布时间】:2013-02-15 12:08:34
【问题描述】:

我有一个表格

    <form id="formModLezione" method="post">

现在,我正在尝试这样做:

 var messaggio="";
 var url = "EsistonoIscritti";
 var xmlHttp = new XMLHttpRequest();
 xmlHttp.open("POST", url, false);
 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlHttp.send("id="+id);
 xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) // COMPLETED
{   if (xmlHttp.status == 200) // SUCCESSFUL
        {var str = xmlHttp.responseText;
         if (str.localeCompare("ko") != 0) {//ci sono utenti iscritti
             var utenti=JSON.parse(str);
             messaggio+="A questa lezione sono iscritte le seguenti persone, avvertile!\n";
      for(var i=0;i<utenti.length;i++){
        messaggio+=(i+1)+") "+utenti[i].nome+" "+utenti[i].cognome+" Tel."+utenti[i].telefono+"\n";
      }
        messaggio+="Vuoi procedere?";
        alert("messaggio");
         }
    } else {
        alert("An error occurred while communicating with the server.");
            }
    }
};
$("#formModLezione").on("submit","return confirm('"+messaggio+"');");
$("#formModLezione").attr("action","ModificaLezione?id="+id);

当我单击表单的提交按钮时,对 servlet ModificaUtente 的调用有效,但没有显示任何警报! 有人知道为什么吗? 谢谢!

【问题讨论】:

  • 如果你使用jquery,为什么不使用内置的ajax()函数呢?
  • @martincarlin87 我只使用ajax 调用来构建变量messaggio。好的,我必须使用 ajax 函数,但它不能很好地工作?
  • 不,只是使用 jquery 函数可能比您现在使用的旧方法要容易得多。
  • @martincarlin87 但问题是 onsubmit,而不是 ajax 调用。
  • 是的,我只是觉得调试起来更容易。您的网址对我来说看起来很奇怪,EsistonoIscritti 不应该是某种脚本,例如esistonoiscritti.php?另外,您使用的是什么版本的 jQuery?我认为on 仅适用于 jQuery > 1.7。你的控制台有什么错误吗?

标签: jquery ajax forms submit confirm


【解决方案1】:

不确定是否存在语法问题,但可能是这样的问题?

$("#formModLezione").on("submit", function(){
    confirm(messagio);
});

【讨论】:

    【解决方案2】:

    试试

    $("#formModLezione").bind("submit", function(e){
       e.preventDefault();
    
        confirm(messagio);
    });
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2017-05-23
      • 2015-08-14
      • 2018-12-05
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多