2011-11-17 48 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標記。

相關問題