首页 > 解决方案 > Javascript中的switch语句附加它需要一个分号并且不执行

问题描述

我试图在对特定 Div 的 Ajax 调用上附加一些 HTML 元素。但是,我得到了日期,并尝试使用回调函数在追加中进行切换,以便我可以使用该开关。如果没有开关,我的代码可以很好地执行,但是当我在附加中插入开关时,它会显示预期的分号或预期的(JS)表达式

append方法里面的switch有什么问题?

这是我的代码

             function showCategoryJobs() {
    var categoryId = $('#search-by-dropdown').val();


    $.ajax({
        url: '@Url.Action("SearchByCategory", "AllJobs")',
        type: 'GET',
        dataType: 'json',
        data: { category: categoryId },
        success: function (posts) {

            $(".job-container").html("");
            //$(".job-container").load(" .job-container");

            $.each(posts.FindJobs, function (x, post) {

                var newdate = new Date(post.PostedDate + 'Z');


                $(".job-container").append(function () {
                    `


                        <li class="separate-job" id="All-Jobs-Id" value="` + post.jobId + `">
                            <div class="content-li-All-Jobs">
                                <h2 class="content-li-All-headline" id="headline-for-Update">`+ post.Headline + `</h2>
                                <a class="btn btn-success bid-for-job" value="`+ post.jobId + `" href="#">Bid now</a>
                                <div class="user">
                                    <blockquote class="blockquote">
                                        <p class="mb-0">
                                            <div class="about-job">`+ post.About + `</div>
                                        </p>
                                        <div class="blockquote-footer">
                                            <cite>-`+ post.Username + `</cite>
                                        </div>
                                    </blockquote>
                                </div>
                                <div class="pictures-li">
                                   `+ $.map(post.JobPictures, function (i, pictures) {

                            if (i != null) {
                                return `<div class="separate-pic">
                <img class="posted-pic" src="data:image/jpg;base64,${i.JobImageContentBytes}" alt="" />
            </div>`;
                            }
                        }).join("") + `
                                </div>

                                <div class="job-date-li">
                                    Posted `+  switch (newdate.getDate()) {
                                                    case 1:
                                                    case 21:
                                                    case 31:
                                                        newdate.getDate(); + '<text>th</text>'
                                                        break;
                                                    case 2:
                                                    case 22:
                                                        newdate.getDate(); + '<text>th</text>'
                                                        break;
                                                    case 3:
                                                    case 23:
                                                        newdate.getDate(); + '<text>th</text>'
                                                        break;
                                                    default:
                                                        newdate.getDate(); +'<text>th</text>'
                                                        break; }; + ` ` + newdate.getMonth() + ` ` + newdate.getFullYear() +
                        `

                                </div>
                                <div class="-job-town">Area | <span class="city">`+ post.JobCity + `</span></div>
                            </div>
                        </li>



                            `
            });

            });



        }
    });
}

标签: javascriptjqueryhtmlasp.net-mvc

解决方案


getDate() 后删除分号


推荐阅读