逐个使用名称attirbute获取文本值

时间:2016-02-05 14:31:16

标签: javascript jquery

我有3个html元素

<h3 contenteditable class="education" name="education[index][University]>1</h3>
<span contenteditable class="education" name="education[index][time]>2</span>
<p contenteditable class="education" name="education[index][description]>3</p>

所以我希望通过javascript使用他们的name属性逐个获取h3-span-p顺序的文本值。每个值后我得到如何跟随这样的新行

1
2
3

先谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

$(function() {
  var output = $('[name^="education[index]"]').map(function() {
    return $(this).html();
  }).get().join('\n');
  alert(output);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 contenteditable name="education[index][University]">1</h3>
<span contenteditable name="education[index][time]">2</span>
<p contenteditable name="education[index][description]">3</p>

<强>更新

如果您有教育课程,那么您可以拨打$('.education').map...