jQuery $.each(someData.split(',') => 返回连接的字符串,而不是项目数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4569271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:30:21  来源:igfitidea点击:

$.each(someData.split(',') => returns concatenated string, instead of array of items

jqueryasp.net-mvcjson

提问by Muaz Khan

I'm using asp.net mvc - ajax with jQuery... I've a model type named "Books" that contains a property "TableOfContents" this property contains data in the following format:

我正在使用 asp.net mvc - ajax 和 jQuery ......我有一个名为“ Books”的模型类型,它包含一个属性“ TableOfContents”,这个属性包含以下格式的数据:

TableOfContents = "1,2,4,6,9,17,28";

Json action method, that reuturn Book object look like this:

Json 动作方法,返回的 Book 对象如下所示:

public JsonResult GetBook(int id) {
    return Json(_bookRepository.Current(id), .....AllowGet);
}

Following style of list images that I want to display.

遵循我要显示的列表图像的样式。

alt text

替代文字

In C# (Razor) I can do this,

在 C# (Razor) 中,我可以做到这一点,

var splitted = Model.TableOfContents.Split(‘,');
@foreach(var number in splitted) {
     <li><img src=”@Url.Content(“~/Content/Images/img-“ + number + “.gif”)” /> </li>
}

This code 100% works and shows images as shown in the above image.

此代码 100% 有效并显示如上图所示的图像。

The same thing I want to done with jQuery because I'm using ASP.NET MVC Ajax with jQuery. Here is the jQuery script through with I get data from MVC via jQuery.

我想用 jQuery 做同样的事情,因为我使用 ASP.NET MVC Ajax 和 jQuery。这是我通过 jQuery 从 MVC 获取数据的 jQuery 脚本。

<script type="text/javascript">          
        function GetBook(id) {
            var url = '@Url.Content("~//Monthly/CurrentBook?id=")' + id;

            $.post(url,
            null,
            function (book) {
                $('#bookResult' + book.ID).html(
                '<a href="@Url.Content("~/BookDetails/")' + book.ID + '">Click to View Details</a>'
                + '<div><p style=" text-align:center;">'
                + '<a href="' + monthly.URL + '"><button style="background-image:url(@Url.Content("~/Content/Images/download-pdf.gif")); width:200px; height:70px;" /></a>'

+ '**<!-- Here I want to use jQuery Code for displaying Table of content Images -->**'

                + '</p></div>');                
            },
           'json'
       );
        }

</script>

I used jQuery code like this:

我使用了这样的 jQuery 代码:

$.each(book.TableOfContents.split(','), function(number) {
  + '<li><img src="img-' + number + '.gif" /></li>'
}

But it displays result as: "1,2,3,17,90" (in string format instead of displaying images)

但它将结果显示为:“1,2,3,17,90”(以字符串格式而不是显示图像)

In ASP.NET MVC Razor, I can display list of images like this:

在 ASP.NET MVC Razor 中,我可以显示这样的图像列表:

  var splitted = Model.TableOfContents.Split(‘,');
        @foreach(var number in splitted) {
             <li><img src=”@Url.Content(“~/Content/Images/img-“ + number + “.gif”)” /> </li>
        }

http://alhadith.cz.cc(this webite's main page displays list of images with ASP.NET MVC Razor)

http://alhadith.cz.cc(该网站的主页显示了带有 ASP.NET MVC Razor 的图像列表)

回答by Greg

If you have

如果你有

var book={TableOfContents:"1,2,4,6,9,17,28"};

as your object with data then in <head>add:

作为带有数据的对象,然后<head>添加:

$(function(){

    $('#list').empty();
    var TableOfContentsSplit=book.TableOfContents.split(',');
    $.each(TableOfContentsSplit,function(number){
          $('#list').append('<li><img src="img-'+TableOfContentsSplit[number]+'.gif" /></li>\n');
    });

});

and in <body>add:

<body>补充说:

<ul id="list"></ul>

At the end you'll get:

最后你会得到:

<ul id="list">
<li><img src="img-1.gif"></li>
<li><img src="img-2.gif"></li>
<li><img src="img-4.gif"></li>
<li><img src="img-6.gif"></li>
<li><img src="img-9.gif"></li>
<li><img src="img-17.gif"></li>
<li><img src="img-28.gif"></li>
</ul>

Cheers G.

干杯G。