How to save Quill.js values to Database Laravel 5.6

…衆ロ難τιáo~ 提交于 2020-08-09 08:53:32

问题


I'm trying to use Quill.js - Your Powerful Rich Text Editor for my project in laravel.

But, since quill uses:

<div id="editor"></div> || <div id="editor" name="body"></div>

Instead of a regular old:

<textarea id="editor" name="body"></textarea>

$post->body = $request->input('body'); won't work.

What do I use to save the information I get from a div with the id of #editor into a database.


回答1:


Use the following in JavaScript:

var content = document.querySelector("#editor").innerHTML

Then append that to your form input before its submitted.

You can also get it directly from the quill instance via:

quill.root.innerHTML



回答2:


Add a hidden input :

<input type="hidden" name="body"/>

Js code :

var form = document.getElementById("FormId"); // get form by ID

form.onsubmit = function() { // onsubmit do this first
                             var name = document.querySelector('input[name=body]'); // set name input var
                             name.value = JSON.stringify(quill.getContents()); // populate name input with quill data
                             return true; // submit form
                           }

To set contents to quill do this :

quill.setContents({!! $post->body !!});


来源:https://stackoverflow.com/questions/49018821/how-to-save-quill-js-values-to-database-laravel-5-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!