我想根据字符串(从php)中的参数数组为TagEditor(或其他具有参数的插件)插件设置动态参数,因此我有一个div。
<textarea class="tag-editor" type="text" name="xxxx" data-attributes-js="{placeholder:'1,2,3,4',other_para,:true,....}"></textarea>
因此,在JS中,我想获取这些参数并返回经过评估的值,以便tagEditor可以处理这些动态参数。
var $input = $('.tag-editor');
$input.tagEditor( $input.attr("data-attributes-js") );
但是这行不通,我该怎么做?
答案 0 :(得分:0)
您可以在此处更改一些内容,例如:-
$(function(){
const $input = $('.tag-editor');
const jsString = $input.attr("data-attributes-js");
const jsObj = JSON.parse(jsString);
console.log(jsObj);;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="tag-editor" type="text" name="xxxx" data-attributes-js='{"placeholder":"1,2,3,4","other_para":true,"another_para":5}'></textarea>
答案 1 :(得分:0)
不知道您的PHP服务器具有什么功能,但一般来说:
.tag-editor()
/* Server Back-end */
/*
this is not code for server -- it's just to provide the JSON string that would come
from a server for demonstration purposes.
*/
let object = {initialTags: ["object", "json-stringify", "data", "json-parse", "tag-editor"]};
let data = JSON.stringify(object);
/* Client Front-end */
$(".tag-editor").data('attributes-js', data);
data = $(".tag-editor").data('attributes-js');
object = JSON.parse(data);
$(".tag-editor").tagEditor(object);
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tag-editor/1.0.20/jquery.tag-editor.min.css" />
<textarea class="tag-editor"></textarea>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tag-editor/1.0.20/jquery.tag-editor.min.js"></script>