2015-09-09 41 views
1

我在HTML一些隱藏的文字,我想表明,當我點擊一個按鈕,但在同一時間,我想的是後面的按鈕生成C#代碼顯示一些文本與按鈕 - JavaScript的

我用此代碼創建JavaScript函數

function toggle() { 

var ele = document.getElementById("dpt"); 
var text = document.getElementById("Button1"); 
if(ele.style.display == "block") { 
     ele.style.display = "none"; 
    text.innerHTML = "show"; 
} 
else { 
    ele.style.display = "block"; 
    text.innerHTML = "hide"; 

,這是在HTML文本和按鈕

 <td id="dpt" style="display: none"> <b>Dias previstos de trabalho:</b> <asp:Label ID="Lbl_DiasTrab" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; </td> 
    <td id= "md" style="display: none"> <b>Media Diária:</b> <asp:Label ID="Lbl_Media" runat="server"></asp:Label></td> 
    <td id= "nrmed" style="display: none"> <b>Número de Médicos:</b> <asp:Label ID="LblNrMedicos" runat="server"></asp:Label></td> 
    <td id= "indcob" style="display: none"> <b>Índice de Cobertura:</b> <asp:Label ID="LblIndCob" runat="server"></asp:Label></td> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Ver" Width="74px" Height="30px" /> 

同時,我做到了,但是當我在按鈕上點擊刷新頁面

我用這個代碼的HTML

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="toggle()" Text="Ver" Width="74px" Height="30px" /> 
+1

你需要檢查你的元素。後面的代碼可能會在元素的ID之前添加諸如「MainContentPlaceholder_YOURID」之類的內容。 –

回答

0

@按是正確的,然後再將其發送給客戶端的ID將被修改服務器端。相反,通過向thistoggle,這將是你的按鈕的引用:

的JavaScript:

function toggle(text) { 

    var ele = document.getElementById("dpt"); 
    if(ele.style.display == "block") { 
     ele.style.display = "none"; 
     text.innerHTML = "show"; 
    } 
    else { 
     ele.style.display = "block"; 
     text.innerHTML = "hide"; 
    } 
} 

HTML:

<td id="dpt" style="display: none"> <b>Dias previstos de trabalho:</b> <asp:Label ID="Lbl_DiasTrab" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; </td> 
<td id= "md" style="display: none"> <b>Media Diária:</b> <asp:Label ID="Lbl_Media" runat="server"></asp:Label></td> 
<td id= "nrmed" style="display: none"> <b>Número de Médicos:</b> <asp:Label ID="LblNrMedicos" runat="server"></asp:Label></td> 
<td id= "indcob" style="display: none"> <b>Índice de Cobertura:</b> <asp:Label ID="LblIndCob" runat="server"></asp:Label></td> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="toggle(this)" Text="Ver" Width="74px" Height="30px" /> 
+0

我不理解對不起 –

+0

@MaraPimentel我更新了代碼以顯示完整的示例。 – Grinn

+0

它不起作用..它不斷刷新頁面 –