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

javascript - How to undo the last checked checkbox? - Stack Overflow

programmeradmin1浏览0评论

Hi I created a javascript function to check if the number of modules selected is greater than a given value. So each time a checkbox is called the function is called, and the function goes through all the checkboxes and calculates the total to see if it's greater. But the problem is when the user checks the checkbox and if the total credits is greater than the value, I want to set the checkbox as checked=false. But I don't which checkbox to undo. Is there any undo last click function in javascript?

Hi I created a javascript function to check if the number of modules selected is greater than a given value. So each time a checkbox is called the function is called, and the function goes through all the checkboxes and calculates the total to see if it's greater. But the problem is when the user checks the checkbox and if the total credits is greater than the value, I want to set the checkbox as checked=false. But I don't which checkbox to undo. Is there any undo last click function in javascript?

Share Improve this question asked Mar 11, 2009 at 12:43 johnnaplesjohnnaples 4071 gold badge5 silver badges7 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8

Not really, but you could fake it easily enough by saving the last box clicked. This code may not work verbatim, but you get the idea:

<script>
var last_checked_box;

function onBoxClicked( box ) {
   if ( box.checked ) last_checked_box = box;
}

function undoLastBox() {
   if ( last_checked_box ) last_checked_box.checked = false;
}
</script>

<input type="checkbox" id="box1" onClick="onBoxClicked(this)"/>
...

Using jQuery, you could do something like:

var MAX_CREDITS = 50; // just as an example
$("input[type=checkbox]").change(function (){
  var totalCredits = 0;
  $("input[type=checkbox]").each(function (){
    // augment totalCredits accordingly to each checkbox.
  });

  if(totalCredits > MAX_CREDITS){
    $(this).removeAttr("checked");
  }
});

If you've never used jQuery before, this surely is like a pain for your eyes; but as you can see, it's very powerful and your problem can be solved in few lines. I'd remend you learning it and giving it a try ;)

I know this isn't the answer you were looking for, but wouldn't it be a better user experience to disable all unchecked checkboxes when the maximum number of checks has been reached?

I dont think there is, but you can always save a reference to the last checked box, so when you have to un-check it, you have it right there.

I suggest either testing if the max number of objects has been checked and if true remove the check all in the same method or you set the value to some hidden text box to the id of your last checked check box so that you can reference it again.

Here's a minimal, but fully functional and jQuery-less solution:

<script>
function isBox(element) {
    return element.form && element.form === document.forms[0] &&
        element.nodeName.toLowerCase() === 'input' &&
        element.type === 'checkbox';
}

function remaining(dR) {
    var field = document.forms[0].elements[0],
        value = +field.value;

    if(dR) return field.value = value + dR;
    else return value;
}

function listener(event) {
    var box = (event && event.target) ||
        (window.event && window.event.srcElement);

    if(isBox(box)) {
        if(box.checked) {
            if(remaining() > 0)
                remaining(-1);
            else box.checked = false;
        }
        else remaining(+1);
    }
}

if(document.addEventListener)
    document.addEventListener('click', listener, false);
else if(document.attachEvent)
    document.attachEvent('onclick', listener);
</script>
<form>
 <p>remaining: <input type="text" readonly="readonly" value="2"></p>
 <p><input type="checkbox" id="b1"><label for="b1">box1</label></p>
 <p><input type="checkbox" id="b2"><label for="b2">box2</label></p>
 <p><input type="checkbox" id="b3"><label for="b3">box3</label></p>
</form>
发布评论

评论列表(0)

  1. 暂无评论