如果选中复选框,则删除类

时间:2011-09-22 09:18:47

标签: php javascript

我希望onload<span><checkbox>为空:

<span id="checkboxx_<?php  echo $nrs['id'];   ?>" class="highlight" > 
   <input type="checkbox" class="checkboxx" id="checkboxx_<?php  echo $nrs['id'];   ?>" style="padding: 0px; margin: 2px;"   <?php echo $value == 1 ? 'checked="checked"' : ''?>>                       
</span>

我试过这个

<?php
if($value==1)
{
?>
  <script>
    if (this.checked) {
      document.getElementById("checkboxx_<?php  echo $nrs['id'];   ?>").className = "";
    }
  </script>
<?php
} else {
?>
   <script>$j("#"+pos).addClass("highlight");</script>
<?php
}

但这不起作用。

2 个答案:

答案 0 :(得分:2)

document.getElementById("MyElement").className =
document.getElementById("MyElement").className.replace(/(?:^|\s)MyClass(?!\S)/ , '')

答案 1 :(得分:0)

实际上,您的代码checkboxx_<?php echo $nrs['id']; ?>中的span和复选框都有重复的ID,而且此代码也有错误且无法正常工作

<script>if (this.checked) {
            document.getElementById("checkboxx_<?php  echo $nrs['id'];   ?>").className = "";
            }
        </script>

我认为使用这样的smth会更好:

<span class="<?php echo ($value !== 1 ? 'highlight' : '') ?>" > 
   <input type="checkbox" class="checkboxx" style="padding: 0px; margin: 2px;"   <?php echo $value == 1 ? 'checked="checked"' : ''?>>                       
</span>
相关问题