【问题标题】:jQuery form submit with delegate带有委托的jQuery表单提交
【发布时间】:2013-11-01 11:20:04
【问题描述】:

我有一个工作 jquery 代码:

  • 从具有相同 id 的多个表单中提交特定表单
  • 对 textarea 进行一些验证,禁用提交。
  • 明文并显示成功或失败消息。

但是 - 我在显示消息时遇到问题,没有输入文本时的验证消息会正确显示到特定的表单 div。但是成功/失败消息没有显示给特定的 div,它总是显示给第一个表单 div。

$('input[type="submit"]').attr('disabled','disabled');
 $('#replytext').keyup(function() {
    if($('#replytext').val() != '') {
       $('input[type="submit"]').removeAttr('disabled');
    }
 });
    $(document).ready(function(){
        //$("#replyForm").submit( function () {
        $('#profile-posts-updates').delegate('#replyForm', 'submit', function() {
        //if($(this).find('#replytext').val()=='')
        if(jQuery(this).children("#replytext").val()=='') {  $(this).next('#replyerror').html('Write your message to post!').css("color","red"); return false;}
         //$('#imgbtn').show();
         //$('#post').hide();
          $.post(
           'Process.php?in=DataHandler',
            $(this).serialize(),
            function(data){
            if(data === 'Success'){
                $('#replytext').val('');
                //jQuery(this).children("#replytext").val('');
                //$("#postbox").fadeOut(50);
                //$("#postbtn").fadeIn(400);
                $("#replyerror").html('Your message has been successfully posted !').css("color","green");
                //$(this).children("#replyerror:first").html('Write your message to post!').css("color","red");
                }else{
                $("#replyerror").html('There was some problem please try again!').css("color","red");
                }
            //$('#imgbtn').hide();
            //$('#post').show();
            }
          );
          return false;   
        });   
    });

【问题讨论】:

  • multiple forms having same id -> id 属性在 DOM 中应该是唯一的。

标签: php jquery


【解决方案1】:

这是您的问题所在(部分)

“具有相同id的多个表单”

每个 HTML DOM 元素必须有一个唯一的 ID,否则它不是有效的 HTML

【讨论】:

  • 但我相信使用 jquery 你有一个解决方案(解决方法?)来处理这个问题。由于 mysql while 循环动态创建表单,因此很难为每个表单提供表单 ID,并创建 jquery 函数。任何帮助都将得到解决。
  • @user2944727,无效标记没有“解决方法”。每个id 必须是唯一的,否则使用class。顺便说一句 - jQuery 只会识别 idfirst 实例。
猜你喜欢
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2011-03-28
相关资源
最近更新 更多