jQuery:连接来自两个元素的值 [英] Jquery : Concatenate Values From Two Elements

查看:64
本文介绍了jQuery:连接来自两个元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点卡住,试图在Jquery中实现某些目标,想知道是否有人可以提供帮助.

bit stuck try to achieve something in Jquery and wondered if anyone can assist..

我正在创建自己的就地编辑功能,在其中单击编辑按钮,然后将我的定义列表的内容交换为表单...预填充了数据.类似于

I am creating my own edit in place function, where you click on an edit button, and the content of my definition list gets swapped for a form...prefilled with data. Similar to this

除了每个可编辑部分(用户注释)都被标记之外,一切都很好,并且可以有多个标签,就像在stackoverflow上一样...所以我为每个注释输出标签的HTML都是这样

All is fine apart from each editable section (user comments) is tagged, and can have multiple tags, much like here on stackoverflow...So my HTML to output the tag for each comment is as so

 <dl id='comment_id'>
  <dt class="comment title">#i.getsTitle()#</a></dt>
           // Other info
    <dd class="categories">
        <dl>
      <dt>Tags:</dt>
    <cfloop array="#i.getCategory()#" index="ii">
     <dd class="category"><a href="">#ii.getsCategory()#</a></dd>
 </cfloop>
  </dl>
   </dd>

因此,我将类别或标签嵌套在由循环控制的定义列表中.

So i nest my categories or tags in an a definition list, controlled by a loop.

到目前为止,我一直试图使用Jquery来获取这些类别的内容,以便在单击以进行编辑时,类别表单字段将被该注释的现有标签预先填充....

What i have tried to do so far is grab the content of these catergories using Jquery, so that when you click to edit, the category form field will be prefilled with the existing tags for that comment....

$('.edit').click(function(){
  // Grab the text for all categories
 var sCategory = $(this).parents('dl').find('dd.categories dl dd.category').text();

 //Build a form and prefill the category form field with the sCategory Variable
 form + '' // Other Data to build form 
 form += '<dl><input name="sCategory" type="text" value="' + sCategory + '" /></dl>'

 // Show edit form prefilled with appropriate content
 $('dl#comment_id).(form);

这有效,但是它显示该条目的所有类别并排显示,没有空格..例如"JqueryColdfusionValidation".我想知道如何将其显示为"JqueryColdfusionValidation"....我猜这里需要.each函数,但是在实现上有些困难

This works, but it displays all the categories for that entry next to each other, with no spaces....eg "JqueryColdfusionValidation". Was wondering how to display it as so "JqueryColdfusionValidation"....Im guessing the .each function is required here, but a bit stuck on how to implement

非常感谢

推荐答案

Map()很好对于这种事情.试试这个:

Map() is good for this sort of thing. Try this:

var sCategory = $(this).parents('dl').find('dd.categories dl dd.category').map(function() {
  return $(this).text();
}).get().join(' ');

这篇关于jQuery:连接来自两个元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆