“检查"多个复选框单击 &拖? [英] "Check" Multiple Checkboxes With Click & Drag?

查看:19
本文介绍了“检查"多个复选框单击 &拖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满复选框的表格,如下所示:

I have a table filled with checkboxes like so:

我希望能够按住鼠标并拖动以激活多个复选框.我不知道从哪里开始:/我搜索了答案,但只找到了 另一个话题,有人问怎么做,但没有答案.

I'd like to be able to keep my mouse held down and drag to activate multiple checkboxes. I don't have the slightest clue where to start with this :/ I searched for an answer, but only found another thread of someone asking how to do it, but with no answers.

HTML:

<table>
  <tbody>
    <tr>
      <td><input type="checkbox"></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox"></td>
    </tr>
    <!-- Repeat tr 2x -->
  </tbody>
</table>

jsFiddle:https://jsfiddle.net/CSS_Apprentice/ge1zx2yg/

另外,我更愿意保留 <input type="checkbox"> 模型,因为改造我的系统会很耗时,但如果不是这样,我愿意接受其他选择不可能.任何帮助将不胜感激!

Also, I'd prefer to keep the <input type="checkbox"> model because reworking my system would be time consuming, but I'm open to other options if this isn't possible. Any help will be greatly appreciated!

推荐答案

<table>
  <tbody>
    <tr>
      <td><input id=1 onmouseover='check(1)' type="checkbox"></td>
      <td><input id=2 onmouseover='check(2)' type="checkbox"></td>
      <td><input id=3 onmouseover='check(3)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input id=4 onmouseover='check(4)' type="checkbox"></td>
      <td><input id=5 onmouseover='check(5)' type="checkbox"></td>
      <td><input id=6 onmouseover='check(6)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input id=7 onmouseover='check(7)' type="checkbox"></td>
      <td><input id=8 onmouseover='check(8)' type="checkbox"></td>
      <td><input id=9 onmouseover='check(9)' type="checkbox"></td>
    </tr>
  </tbody>
</table>

<script>
  function check(id)
  {
    if(mouseDown)
      {
        document.getElementById(id).checked = 1-document.getElementById(id).checked;
        // document.getElementById(id).checked = true;
        // ^ If you only want to turn them on, use this.
      }
  }
  
  var mouseDown = 0;
  document.body.onmousedown = function()
  { 
    ++mouseDown;
  }
  
  document.body.onmouseup = function()
  {
  --mouseDown;
  }
  // Credit to http://stackoverflow.com/questions/322378/javascript-check-if-mouse-button-down
</script>

或者,使用下面的代码来避免 ID:

Or, alternatively, use the code beneath to avoid IDs:

<table>
  <tbody>
    <tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
	<tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
  </tbody>
</table>

<script>
  function check(box)
  {
    if(mouseDown)
      {
        box.checked = 1-box.checked;
		// box.checked = 1;
        // ^ If you only want to turn them on, use this.
      }
  }
  
  var mouseDown = 0;
  document.body.onmousedown = function()
  {++mouseDown;}
  document.body.onmouseup = function()
  {--mouseDown;}
  // Credit to http://stackoverflow.com/questions/322378/javascript-check-if-mouse-button-down
</script>

这篇关于“检查"多个复选框单击 &amp;拖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆