2011-08-09 238 views
-1

你好我正在使用此代码发布我的应用程序消息墙上的重要消息,我正在使用复选框在消息墙上发布重要消息。问题在于,在发布重要消息之后,它将再次发布重要消息而不点击复选框,意味着在发布重要消息之后该复选框保持为真。请帮助我。 和我的代码是:需要JavaScript帮助

<div class="checkbox" id="checkboxShow" name="userCheckList1" style="display:block;" onclick="importantChkBoxChange(userCheckListShow.checked);"> 

    <input type="checkbox" name="userCheckList1" id="userCheckListShow" unchecked="unchecked" 
    onchange="importantChkBoxChange(userCheckList.checked);"/> 
    </div> 

    <div class="checkbox" id="checkboxHide" name="userCheckList1" style="display:none;" onclick="importantChkBoxChange(userCheckListHide.checked);"> 

    <input type="checkbox" name="userCheckList1" id="userCheckListHide" unchecked="unchecked" 
    onchange="importantChkBoxChange(userCheckListHide.checked);"/> 
    </div> 
    <span class="fl" style="margin:3px 0px 0 0;" ><@spring.message "label.employee.home.Important"/></span> 

及用于此的脚本是:

function importantChkBoxChange(chkBoxStatus){ 

     if(chkBoxStatus == true){ 
      document.forms['messageWallForm'].elements['important'].value="checked"; 
     }else if(chkBoxStatus == false){ 
      document.forms['messageWallForm'].elements['important'].value="unchecked"; 
     } 
    } 
+3

请问您可以开始给您的问题描述性标题。 “需要JavaScript帮助”并不能告诉我们实际的问题。你的三个题目“IE中的jQuery问题”和两个题为“Mozilla Firefox中的JavaScript错误”的问题都没有。 – Bart

+0

@Raman:我想通过格式化和重写这个问题来提供帮助,但是这个问题表达得很差,我甚至不知道要改变它。什么是userCheckList,UserChecklistHide和UserCheckListShow? – mplungjan

回答

0

好吧,首先unchecked没有输入的属性。如果您希望具有XML意识,您可以有<input type="checkbox" checked="checked" />,或者您可以简单地省略=“checked”:<input type="checkbox" checked />,但未选中是默认设置,因此无法向我们提供unchecked属性。其次,我注意到有变量userCheckListHide和userCheckListShow,但我没有看到这些变量的声明。你确定它们存在吗?在创建输入后调用了userCheckListShow = document.getElementById("userCheckListShow")?否则,你正在处理空值。

第三,有了复选框和单选按钮,您对价值不感兴趣,就像您对checked感兴趣一样。你可以重新写你的函数:

// changed the variable name so that it wouldn't scroll. Your var. name was fine. 
function importantChkBoxChange(stat){ 
    // since your test is if(checked) set it to checked otherwise set it to 
    // unchecked, you can simply set the checked property directly. 
    document.forms['messageWallForm'].elements['important'].checked = stat; 
} 

作为一个侧面说明,如果你想这反映是否任何的CB在你的问题被选中,那么这两个onchange处理程序应该是:

importantChkBoxChange(userCheckList.checked || userCheckListHide.checked); 
0

我不知道从哪里开始,但这里有几点提示。

  • 通过更改它的值不选中/取消选中复选框。它有一个checked属性。像document.getElementById('checkBoxId')。checked = true
  • 使用DOM标识符引用您的DOM元素。理想情况下。
  • 如果(chkBoxStatus ==真)是多余的
  • 有一个为复选框
  • ,如果你想传递一个事件(如平变化)复选框的js函数的检查状态
  • ,你可以做任何选中属性像onchange =“foo(this.checked)”