2016-07-21 31 views
0

我嘗試後使用jQuery和AJAX的形式,將一些輸入元素與一個div(從this WYSIWYG plugin)的html我的網頁上一起發送:添加格的內容,形成數據

 $("#group-add-btn").click(function(){ 
     var data = $("#group-add").serialize(); // form 
     //This is the line vvv 
     data["description"] = $("#description-editor").cleanHtml(); 
     // ^^^ 
     console.log(data); 
     $.ajax({ 
      type: 'POST', 
      url: base_url+"/some/method", 
      dataType:'json', 
      encode:'true', 
      data:data, 
      success: function(res){ 
       console.log(res); 
       if(res.errors){ 
        $("#group-add .feedback").html(res.errors); 
       } 
       else { 
        $("#group-container-box .panel-body").append(res.new_content); 
        console.log(res.new_content); 
        $("#group-add").trigger("reset"); 
       } 
      }, 
      error: function(jqXHR, errorThrown){ 
       console.log('jqXHR:'); 
       console.log(jqXHR); 
       console.log('errorThrown:'); 
       console.log(errorThrown); 
      } 
     }) 
    }); 

我試着這也:

var data = $("#group-add").serialize() + "&description=" = $("#description-editor").html(); 

但這切斷後,意想不到的是達到了含量「:」(不知道這是預期的行爲)。

什麼是包含HTML作爲提交的數據的一部分的最佳方式?我使用Code Igniter,因此我希望能夠使用$this->input->post("description")(類似於$_POST['description'])訪問此數據。

回答

0

我發現在我的html中有一個和號(&)錯誤,可以使用encodeURIComponent()進行編碼。

答案:

var data = $("#group-add").serialize() + "&description="+encodeURIComponent($("#description-editor").html()); 

我投了一個副本。