te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - jQuery Get value from class attribute of multiple elements in array without loop - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Get value from class attribute of multiple elements in array without loop - Stack Overflow

programmeradmin1浏览0评论

I have multiple Select dropdown elements with a single class .select

<select class='select' name='select1' id='select1'>
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
</select>

<select class='select' name='select2' id='select2'>
  <option value='4'>4</option>
  <option value='5'>5</option>
  <option value='6'>6</option>
</select>

<select class='select' name='select3' id='select3'>
  <option value='7'>7</option>
  <option value='8'>8</option>
  <option value='9'>9</option>
</select>

I know it can be achieved with the help of loop like this

var arr = [];
$('.select').each(function () {
   arr.push($(this).val());
});

But I already have so many loops in the code and I'm wondering if is there any way it can be achievable without a loop

Fiddle: /

I have multiple Select dropdown elements with a single class .select

<select class='select' name='select1' id='select1'>
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
</select>

<select class='select' name='select2' id='select2'>
  <option value='4'>4</option>
  <option value='5'>5</option>
  <option value='6'>6</option>
</select>

<select class='select' name='select3' id='select3'>
  <option value='7'>7</option>
  <option value='8'>8</option>
  <option value='9'>9</option>
</select>

I know it can be achieved with the help of loop like this

var arr = [];
$('.select').each(function () {
   arr.push($(this).val());
});

But I already have so many loops in the code and I'm wondering if is there any way it can be achievable without a loop

Fiddle: http://jsfiddle/89cJC/

Share Improve this question asked Mar 19, 2014 at 13:45 riksof-zeeshanriksof-zeeshan 5319 silver badges28 bronze badges 1
  • Any implementation will have loop for this :) You cant avoid – Murali Murugesan Commented Mar 19, 2014 at 13:48
Add a ment  | 

2 Answers 2

Reset to default 14

No, there's no other way to get the value from multiple elements, you have to iterate over the elements to get the value from each of them.

There are other ways to write basically the same code

var arr = $.map($('.select'), function (el) { return el.value; });

or without jQuery

var elems = document.querySelectorAll('.select'),
    arr   = [];

for (var i=elems.length; i--;) arr.push(elems[i].value);

but they all iterate, there's no other way to do that.

You can use jquery $.map function of jquery to deal with elements array

for example

var arr = [];
arr = $.map($(".select"),function(select){
        return $(select).val();
});

console.log(arr);
发布评论

评论列表(0)

  1. 暂无评论