简体   繁体   English

无法访问表单元素

[英]Can't access form elements

my problem is that my variables are not working in JavaScript. 我的问题是我的变量在JavaScript中不起作用。 all variables need names without some character at the beginning, this is the stupid thing... Anyway, I'm trying to make a function that makes "select all checkboxes". 所有变量的开头都需要没有任何字符的名称,这是愚蠢的事情……无论如何,我正在尝试创建一个使“选择所有复选框”成为可能的函数。 It is not working so I looked at the page source/info and found out that the variables were not changing. 它不起作用,所以我查看了页面源代码/信息,发现变量没有更改。

this is my input: 这是我的输入:

echo "<input onclick='checkAll(1);' type='checkbox' name='master'/><br/>";

My function: 我的功能:

function checkAll(i)
{
 for(var i=1; i < <?php echo $num; ?>; i++)
 {
  if(document.demo.master[i].checked == true)
  {
   document.demo.message[i].checked = true;
  }
  else
  {
   document.demo.message[i].checked = false;
  }
 }
}

so yes that's it. 是的,就是这样。 I can tell you that I also tried without the <i> in: checkAll("i") 我可以告诉你,我也尝试了在没有<i>情况下使用: checkAll("i")


EDIT: each checkbox for each message has this code: echo "<input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' /><br/>"; 编辑:每个消息的每个复选框都有此代码: echo "<input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' /><br/>"; echo "<input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' /><br/>";


EDIT: and also, I tried a code once upon a time and it worked on another computer, but on mine it wasnt working. 编辑:而且,我曾尝试过一次代码,它可以在另一台计算机上工作,但在我的计算机上却无法正常工作。 We had the exact same code... Is that normal? 我们有完全相同的代码...这正常吗? What is wrong? 怎么了?

Why not just: 为什么不只是:

function checkAll()
{
 for(var i=0; i < <?php echo $num; ?>; i++)
 {
   document.demo.message[i].checked = true;
 }
}

If you want to toggle the current values: 如果要切换当前值:

function toggleAll()
{
 for(var i=0; i < <?php echo $num; ?>; i++)
 {
   document.demo.message[i].checked = !document.demo.message[i].checked;
 }
}

However, that doesn't seem very useful in practice (if A is checked and B and C are unchecked, how often do you want A unchecked and B and C checked?). 但是,这在实践中似乎不是很有用(如果A被选中,而B和C被选中,那么您希望A未被选中而B和C被选中的频率是多少?)。 I would just have Select All and Unselect All buttons. 我只需要全选和取消全选按钮。

I removed the parameter, changed i to start at 0 (0-indexed), and just had it unconditionally check the box. 我删除了该参数,将i更改为从0(索引为0)开始,只是无条件地选中了该框。 Before you had it backwards, so it would check it if it were already checked, and vice versa. 在您将其退回之前,因此它将检查它是否已被检查,反之亦然。 And you shouldn't need to ever set it false in a checkAll method. 而且,您不必在checkAll方法中将其设置为false。

Also, make the Select All a button: 另外,将“全选”按钮:

echo "<input onclick='checkAll(1);' type='button' name='master' value='Select All' /><br/>";

For this problem I use the power of prototype js lib 对于这个问题,我使用了原型js库的强大功能

take your checkbox <input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' /> 选中您的复选框<input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' /> <input style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' />

add a class 添加课程

<input class='checkall' style='margin-left:-15px;margin-top:20px;' type='checkbox' name='message' value='$rid' />

then 然后

$$('.checkall').each(function(item){
   item.checked = true;
});

or if you want the checked/unchecked option 或者如果您想要选中/未选中选项

$$('.checkall').each(function(item){
  if(item.checked)
    item.checked = false;
  else
    item.checked = true;
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM