2011-11-17 72 views
0

我在我的项目中使用了struts框架。已在我的JSP上使用了一个复选框,一个按钮和一个文本框。以下是所有这些代码。Struts - JSP - 使用Java脚本启用/禁用基于状态复选框的按钮和文本框

<html:text name="FormBean" styleId = "codeERText" property="codeER" onFocus="change_classe_byId(this,'on');" onBlur="change_classe_byId(this,'off');" styleClass="off" maxlength="5"/> 

<input type="button" styleId = "deduireButton" name = "deduireButton" id = "deduireButton" value="<bean:message key="Property.deduire"/>" onClick="javascript:validate('DEDUIRE');"> 

<html:checkbox styleId="idBrodcastCheckbox" property="sentToAll" name="FormBean" onChange="javascript:broadcastValidate();" /> 

要求是当复选框被选中时,它应该禁用按钮和文本框,反之亦然。

以下是在复选框的onChange事件中调用的函数.. 在Application上运行时,它只是给出第一个警报(“Inside Broadcast Validate”)..没有其他警报提出。或文本框被禁用。我相信,它不会进入任何If/Else对。

使用的App Server是Weblogic 8.1。

function broadcastValidate(){ 

    alert("Inside Broadcast Validate"); 
    if(document.forms[0].idBrodcastCheckbox.checked == true) 
    { 
     alert("checked true"); 
     document.forms[0].codeERText.disabled = true; 
     document.forms[0].deduireButton.disabled = true; 
    } 
    else 
    { 
     alert("checked false"); 
     document.forms[0].codeERText.disabled = false; 
     document.forms[0].deduireButton.disabled = false; 
    } 

    alert("First If over"); 

    if(this.document.FormBean.getElementById("idBrodcastCheckbox").checked == true) 
    { 
     alert("checked true 2"); 
     this.document.getElementById("codeERText").disabled = true; 
     this.document.getElementById("deduireButton").disabled = true; 
    } 
    else 
    { 
     alert("checked false 2"); 
     this.document.getElementById("codeERText").disabled = false; 
     this.document.getElementById("deduireButton").disabled = false; 
    } 
    alert("Second If over "); 

    if(document.forms[0].sentToAll.value == true) 
    { 
     alert("checked true - Sent to All"); 
     document.forms[0].codeERText.disabled = true; 
     document.forms[0].deduireButton.disabled = true; 
    } 
    else 
    { 
     alert("checked false - Sent to All"); 
     document.forms[0].codeERText.disabled = false; 
     document.forms[0].deduireButton.disabled = false; 
    } 

    alert("Third If over - Exiting Broadcast Validate"); 
} 

请对此建议一些解决方案。

回答

0

肯定不会是this.document.FormBean.getElementById(),因为getElementById()是文件上的方法。要获取ID的元素,这是我想在这种情况下做的,使用方法:

document.getElementById('idBrodcastCheckbox') 

你不应该需要指定的复选框name属性;默认情况下,假设属性存在于动作的ActionForm上。除非你专门做了不同的事情,否则使用框架的默认设置。

如果您甚至没有收到"First If over"提醒,请检查您的JavaScript控制台。

+0

谢谢戴夫..我修改了代码,并使用document.getElementById('idBrodcastCheckbox') 建议。它现在工作正常..非常感谢:) –

+0

@NiravKhandhedia真棒,很高兴你得到它的工作! –

0

我试过你的密码,document.forms[0].idBrodcastCheckbox.checked返回true。当您说除"Inside Broadcast Validate"之外没有其他信息被警告,可能在第一条警报声明后发生错误。正如戴夫所说,使用你的Javascript控制台来查看任何错误。此外,请检查您是否错过了标记元素应保存在内部的html:form标记。

相关问题