html - "Check"单击并拖动多个复选框?

标签 html checkbox

我有一个表格,里面满是复选框:

enter image description here

我希望能够按住鼠标并拖动以激活多个复选框。我不知道从哪里开始:/我搜索了一个答案,但只找到了 another thread有人问如何去做,但没有答案。

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">模型,因为改造我的系统会很耗时,但如果这不可能,我愿意接受其他选择。任何帮助将不胜感激!

最佳答案

<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:

<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>

关于html - "Check"单击并拖动多个复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754940/

相关文章:

javascript - 单击 href 和单击复选框时选中复选框

html - 使 PDF 帮助文件自动打开

html - 为什么此 css 不将此表单上的提交按钮居中?

php - 以下代码用于更新用户图像。代码显示执行但数据库没有变化

javascript - 获取被点击元素的 id 而无需在 html 中放置任何 js 代码

javascript - 在 Ember JS 中选择动态创建的复选框

javascript - 平滑缩放移动 div 元素

html - 如何在 CSS 中包装复选框表单元素?

android: ListView 中的动态复选框

forms - 带有复选框/单选按钮的电子邮件调查问卷?