【问题标题】:ajax form will submit after second submit press with ckeditorajax 表单将在第二次使用 ckeditor 提交后提交
【发布时间】:2012-12-03 20:51:48
【问题描述】:

我有以下配置,一个使用 ajax 提交的表单和一个使用 ckeditor 的 textarea,因为我使用 ckeditor,表单将在第二次按下按钮时提交,在第一次按下按钮时表单不会提交。

JS

 CKEDITOR.replace('_message_tokenizer', {width:"400", height:"200", toolbar : [['Styles','Font','FontSize','Bold', 'Italic', '-', 'Link', 'Unlink', 'smiley', 'Undo', 'Redo']] });





  $("#envoyer_message_tokenizer").submit(function(){

  if( $.trim($("#_message_tokenizer").val())!="" &&  $.trim($("#contacts").val())!=""){

var contacts=[];
contacts.push($("#contacts").val());

   $.ajax({
  type:"POST",
data:{"action":"envoyer_message_tokenizer", "session_id":<?php echo $session_id['id'];?>, "_message_tokenizer":$("#_message_tokenizer").val(), "_sujet":$("#_sujet").val(), "contacts":contacts} ,
  url: 'messages-ajax.php',
  success: function(data){


    $("#envoyer_message_tokenizer").fadeOut("slow", function(){
      $("#message_tokenizer").children().html(data);

    });

         }
      });
    }
 })

HTML

<form id="envoyer_message_tokenizer" method="post"  action="javascript:;">
<table cellpadding="0"  cellspacing="0" class="padding dotted">
<tr><td valign="top" width="130"><b class="bg">Sujet</b></td>
<td><input type="text" class="_input _input_text" style="width: 399px;" id="_sujet"/></td></tr>
<tr><td valign="top"><b class="bg">Destinataire(s)</b> <span class="ast_oblig">*</span></td>
<td><input type="text" class="_input _input_text"  name="contacts" id="contacts"/></td></tr>
<tr><td valign="top"><b class="bg">Message</b> <span class="ast_oblig">*</span></td>
<td><textarea style="width: 399px;height: 100px;" id="_message_tokenizer"></textarea></td></tr>
<tr class="no_dotted"><td></td><td><input type="submit"  class="_submit" value="envoyer" /></td></tr>
</table>
</form>

我已经尝试过 ckeditor getData 方法,但结果仍然相同。

【问题讨论】:

    标签: jquery ajax ckeditor


    【解决方案1】:

    您需要在提交处理程序中使用event.preventDefault()return false 来防止浏览器默认提交表单

    $("#envoyer_message_tokenizer").submit(function(event) {
    
         /*use either method*/
         /* before code*/
        event.preventDefault();  
    
          /* your handler code*/
    
         /* OR after code*/
    
         return false;
    
    })
    

    【讨论】:

    • 还是一样:表单将在第二次按下时提交。
    • 如果从 if 中删除 $.trim($("#_message_tokenizer").val())!="" &amp;&amp; 会发生什么?您可以强制 CKEditor 在 if 之前使用 API 方法更新 textarea,假设这是问题
    • 还需要知道是否正在发送 AJAX ...需要有关 not submitting 的更多详细信息以及您如何知道它没有提交
    • 是的,如果我删除修剪的东西,它在第一次提交时有效,你知道为什么吗?我仍然需要验证这些输入。
    • 寻找更新文本区域的CKEditor方法,在if之前运行它
    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2011-06-17
    相关资源
    最近更新 更多