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 global array - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript global array - Stack Overflow

programmeradmin4浏览0评论

I am having below html code and trying to add new values to global array by onchanging of javascript function.I am trying to do like below way.But it is giving javascript errors.Please suggest anyone how to do this.

<html>
<head>
<script>
var list=[];
function getList(value){

list=list(value);
}
</script>

</head>

<body>

<tr>
<td>
<select name="test" onchange="getList(this)">
<option id="1" value="one">One</option>
<option id="2" value="two">two</option>

</select>
</td>
</tr>
<tr>
<td>
<select name="test1" onchange="getList(this)">
<option id="3" value="three">three</option>
<option id="4" value="four">four</option>

</select>
</td>
</tr>

</body>
</html>

I am having below html code and trying to add new values to global array by onchanging of javascript function.I am trying to do like below way.But it is giving javascript errors.Please suggest anyone how to do this.

<html>
<head>
<script>
var list=[];
function getList(value){

list=list(value);
}
</script>

</head>

<body>

<tr>
<td>
<select name="test" onchange="getList(this)">
<option id="1" value="one">One</option>
<option id="2" value="two">two</option>

</select>
</td>
</tr>
<tr>
<td>
<select name="test1" onchange="getList(this)">
<option id="3" value="three">three</option>
<option id="4" value="four">four</option>

</select>
</td>
</tr>

</body>
</html>
Share Improve this question asked Apr 11, 2011 at 10:27 user569125user569125 1,46313 gold badges29 silver badges40 bronze badges 1
  • How about telling us what the error is? Is the function list defined somewhere (I guess not)? I suggest you read a JavaScript tutorial first: developer.mozilla/en/JavaScript/Guide – Felix Kling Commented Apr 11, 2011 at 10:30
Add a ment  | 

4 Answers 4

Reset to default 8

Change your Javascript to the following

var list=new Array; ///this one way of declaring array in javascript
function getList(value){

list.push(value);//push function will insert values in the list array
}

Adding values to an array is quite simple, all you have to do is call the push(..) method.

Like so:

var list = [ ];
list.push(1);

console.info(list); // Outputs: [ 1 ]
  • Mozilla array reference
  • Mozilla array push reference

The easiest way to add a value to an array in JavaScript, it to use the push method (unless you need to support IE5):

function getList(value) {
  list.push(value);
}

Two things:

  • In your case this would add references to the select elements to the list. That's most likely not what you want. What exactly do you want to add?

  • getList isn't really a suitable name for that. addToList would be probably better.

You're currently passing the entire select element to your function, rather than the selected value. You can pass the selected value like this:

getList(this.options[this.selectedIndex].value)

Then you can use list.push(value) in your function to add the selected value to your array.

发布评论

评论列表(0)

  1. 暂无评论