简体   繁体   English

append()中的JavaScript单引号和双引号-fullcalendar外部事件

[英]JavaScript single quotes and double quotes in append() - fullcalendar external-events

I am creating a drop-down menu which generates several external-events (depending on your choice) that can be drop on the fullcalendar. 我正在创建一个下拉菜单,该菜单会生成多个外部事件(取决于您的选择),这些事件可以放在完整日历上。 I need to append several event divs to another div in the success part of ajax. 我需要在ajax的成功部分中将几个事件div附加到另一个div。

The problem is that I can't properly write the single and double quotes in the string that must be placed in the append function. 问题是我不能在必须放在append函数中的字符串中正确编写单引号和双引号。

The div should be rendered like this after append: div应该在追加之后呈现为:

 <div class='fc-event' data-event='{"id": 1, "title": "This is event 1"}'>My Event 1</div>

Here is the function for listening to changes in drop-down list and generate external-events: 这是侦听下拉列表中的更改并生成外部事件的功能:

$("#category").change(function () {

    $('#listAct').html("");

    $.ajax({
        type: "POST",
        url: 'SearchActivities.php',
        data: { CategoryName: $("#category").find("option:selected").val() },

        success: function (data) {
            var result = $.parseJSON(data);
            var arraysize = result.length;
            var i, ActTitle;

            for (i = 0; i < arraysize; i++) {

                ActTitle = result[i]['ActTitle'];

                var eventdetails = '{"id":1,"title":"This is event 1"}';

                $('#listAct').append('<div class="fc-event" data-event=' + eventdetails + '>' + ActTitle + '</div>');

            }
            /* initialize the external events
             -----------------------------------------------------------------*/
            $('#listAct').find('.fc-event').each(function () {
                // make the event draggable using jQuery UI
                $(this).draggable({
                    zIndex: 999,
                    revert: true,      // will cause the event to go back to its
                    revertDuration: 0  //  original position after the drag
                });
            });
        }
    });
});

I try to place all the necessary values in a varialbe (eventdetails) and then use it in the append content but if you want to place the data-event content directly in the append function there is no problem. 我尝试将所有必要的值放在varialbe(事件详细信息)中,然后在附加内容中使用它,但是如果您想将数据事件内容直接放置在附加函数中,就没有问题。

The drop-down list : 下拉列表:

<form>
    <label style="color: black" for="category">Select Category</label>
    <select id="category" name="category">
        <option value=""></option>
        <option value="Attractions, Nature Park">Attractions, Nature Park</option>
        <option value="Underwater Activities">Underwater Activities</option>
        <option value="Museums and Sites">Museums and Sites</option>
    </select>
</form>

    <div id="listAct">
        <div class='fc-event' data-event='{"id": 1, "randomProperty": "foobar", "title": "This is event 1"}'>My Event 1</div>
    </div>

Change this line 更改此行

$('#listAct').append('<div class="fc-event" data-event=' + eventdetails + '>' + ActTitle + '</div>');

to

$('#listAct').append('<div class="fc-event" data-event=\'' + eventdetails + '\'>' + ActTitle + '</div>');

Using \\' will cause the single quotes to be appended to the string and you'll end up with what you want: 使用\\'将导致单引号被附加到字符串,您将得到所需的结果:

<div class='fc-event' data-event='{"id": 1, "title": "This is event 1"}'>My Event 1</div>

instead of 代替

<div class='fc-event' data-event={"id": 1, "title": "This is event 1"}>My Event 1</div>

Edit: To use a variable called title in the eventdetails variable, use 编辑:要在eventdetails变量中使用名为title的变量,请使用

var eventdetails = '{"id":1,"' + title + '":"This is event 1"}';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM