最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Adding comma after each word in JavaScript - Stack Overflow

programmeradmin3浏览0评论

I have selected tag input and I can choose multiple items using MagicSuggest plugin.

The problem is that the value that I get after the choice is a string separated without a ma.

Like:

MVCPHPASP.Net

I want to change it to:

MVC,PHP,ASP.Net

The result that I get when I choose multiple items :

The string that I get when I alert :

So how can I do that?

I have selected tag input and I can choose multiple items using MagicSuggest plugin.

The problem is that the value that I get after the choice is a string separated without a ma.

Like:

MVCPHPASP.Net

I want to change it to:

MVC,PHP,ASP.Net

The result that I get when I choose multiple items :

The string that I get when I alert :

So how can I do that?

Share edited Jan 31, 2016 at 14:13 Mosh Feu 29.4k18 gold badges93 silver badges141 bronze badges asked Jan 31, 2016 at 13:01 user4851087user4851087 7
  • You don't, unless you have very specific words you are looking for. Almost every tag ponent offers some form of serialization that will do this for you. Since you don't say which one you're using, there's no way to know how that particular one does it. It's more likely you're just grabbing a raw array and not using join. – Dave Newton Commented Jan 31, 2016 at 13:04
  • 4 It would be very useful if you could show us html and javascript relative to this question, that you are using. – Tornike Shavishvili Commented Jan 31, 2016 at 13:08
  • Here it's my js file to get the string inside that textbox Here it's my js file using magic suggest Here it's my html file to display magic suggest – user4851087 Commented Jan 31, 2016 at 13:10
  • As i understand, your values are predefined strings. Would it possible to append those values with "," ? For example: instead of "MVC" you could have "MVC," ? – Tornike Shavishvili Commented Jan 31, 2016 at 13:13
  • yes @blindProgrammer how can i do that – user4851087 Commented Jan 31, 2016 at 13:24
 |  Show 2 more ments

1 Answer 1

Reset to default 5

The right way is to get the data using the plugin's API, not weird parsing.

According the docs you can use the function .getSelection() to get the selected items.

var ms = $('#ms-getValue').magicSuggest({
  data: [
    {
      id: 1,
      name: 'Paris'
    },
    {
      id: 2,
      name: 'New York'
    },
    {
      id: 3,
      name: 'Gotham'
    } 
  ]
});

function getValues() {
  // array
  var selected = ms.getSelection();
  alert(selected.map(function(item) {
    return item.name;
  }).join(','));
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://nicolasbize./magicsuggest/lib/magicsuggest/magicsuggest.css" rel="stylesheet" />
<script src="http://nicolasbize./magicsuggest/lib/magicsuggest/magicsuggest.js"></script>

<div id="ms-getValue"></div>
<button onclick="getValues()">Get Values</button>

Update The result is an Array. So, to get the first (for example) selected object you do: selected[0].name

Update 2

To show all the selected values separated by ma:

selected.map(function(item) {
  return item.name;
}).join(',')
发布评论

评论列表(0)

  1. 暂无评论