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

jquery - Javascript Event for Select element Selection - Stack Overflow

programmeradmin3浏览0评论

I'm looking to find the Javascript Event I need to put into jQuery's .bind function in order to have the function triggered when a selection is made from a <select> element.

At the moment I'm using .bind('change',function() { ...}) but I need the event to trigger when the selected option is chosen again.

Any suggestions?

I'm looking to find the Javascript Event I need to put into jQuery's .bind function in order to have the function triggered when a selection is made from a <select> element.

At the moment I'm using .bind('change',function() { ...}) but I need the event to trigger when the selected option is chosen again.

Any suggestions?

Share Improve this question asked Apr 5, 2010 at 20:56 JP.JP. 5,59415 gold badges64 silver badges106 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 4

I'm currently doing this successfully by clearing the dropdown box selection so that it can be re-selected. It will allow you to trigger the function again by adding this within your function before the end:

$('select option:selected').removeAttr('selected');

Change on select boxes is unreliable anyway. Read:

http://www.quirksmode/dom/events/change.html#t05

I'd probably go for something click (but be suspicious, somebody (--> IE) is going to make your life difficult). Or build something yourself without select.

This is possible, however it is not fully supported. Here is a sample:

html

<select id="selectId">
 <option value="1">A</option>
 <option value="2">B</option>
 <option value="3">C</option>
 <option value="4">D</option>
</select>

jquery

$("#selectId>option").click(function(){
 alert(this.value);
});

here is a mediocre approach to handle ie:

<div id="dropdownWrapper">
 <select id="selectId">
  <option value="1">A</option>
  <option value="2">B</option>
  <option value="3">C</option>
  <option value="4">D</option>
 </select>
</div>

js

var clickCount_guid324fF = 0;
$("#dropdownWrapper").click(function(){
    if(++clickCount_guid324fF % 2 == 0){
        alert($("#selectId").val());
        clickCount_guid324fF = 0;
    }
});

Wouldn't click work? .bind('click',function() { ...})

I once did this using mouseup, and checked where the event originated. If it was an option element, i handled the select. No listening on onchange at all:

<body>
<select id="select0">
    <option value="0">option 0</option>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<script type="text/javascript">
$('#select0').bind('mouseup', function (e) {
    var src = e.target;
    if (src.nodeName.toLowerCase()==='option') {
        doMagicOnSelect(this);
    }
});
function doMagicOnSelect(selectElem) {
    console.log('current value:'+selectElem.value);
}
</script>
</body>

There is no reliable event fired when a selected option is re-chosen.

Whilst on some browsers you can catch events originating from an <option> (which you could use to trap click and keyboard events if you had the patience to try to reproduce a browser's select handling), this is unstandardised and doesn't work in IE (as it uses native Windows widgets to implement select boxes, which don't give that kind of granular access).

If you need to be able to re-fire an event when the same option is chosen again, what you have doesn't really sound like a select box to me; it could perhaps be better replaced with a scripted pop-up div full of buttons. (There are plenty of scripts that will substitute a select box for a scripted div to give you greater control on browsers where JavaScript is available.)

bind the change AND click event

$select.bind("change click", function (event) { // do something   });
发布评论

评论列表(0)

  1. 暂无评论