1

我正在學習如何編寫不引人注目的Javascript,並且我已經能夠處理大部分事情,但是我在使用元素的可見性打開和關閉時遇到問題一個複選框。請記住,我是一名設計師,程序員,大約十八號左右。換句話說,在編程方面我是新手。 (注意:此表格適用於牙髓治療公司轉診表,因此是行業術語)。Unobtrusive JS:使用表單中的複選框切換元素的可見性

另外,請不要提示jQuery;我知道它存在,我經常使用它。我想學習如何在純Javascript中實際做到這一點。這裏是重要的HTML片段(這在asp.NET創建):

<ol> 
<li> 
    <fieldset> 
     <legend>Which of these procedures is required? (select at least one)<span class="required"> *</span></legend> 
     <label id="procedure01"><asp:CheckBox ID="chkEndodonticProcedure01" GroupName="EndodonticProcedure" runat="server" />Consultation for evaluation of tooth</label> 
     <label id="procedure02"><asp:CheckBox ID="chkEndodonticProcedure02" GroupName="EndodonticProcedure" runat="server" />Re-treatment of tooth</label> 
     <label id="procedure03"><asp:CheckBox ID="chkEndodonticProcedure03" GroupName="EndodonticProcedure" runat="server" />Treatment of tooth</label> 
    </fieldset> 
    <ol id="consultation"> 
     <li> 
      <asp:Label ID="lblConsultationToothNumber" AssociatedControlID="txtConsultationToothNumber" runat="server">Consultation tooth number</asp:Label> 
      <asp:TextBox id="txtConsultationToothNumber" CssClass="textbox" runat="server" /> 
     </li> 
     <li> 
      <fieldset> 
       <legend>Do any of these conditions apply?</legend> 
       <label><asp:CheckBox ID="chkToothCondition01" GroupName="ToothCondition" runat="server" />Vague toothache</label> 
       <label><asp:CheckBox ID="chkToothCondition02" GroupName="ToothCondition" runat="server" />Pain, swelling</label> 
       <label><asp:CheckBox ID="chkToothCondition03" GroupName="ToothCondition" runat="server" />Sensitivity to heat</label> 
       <label><asp:CheckBox ID="chkToothCondition04" GroupName="ToothCondition" runat="server" />Sensitivity to cold</label> 
       <label><asp:CheckBox ID="chkToothCondition05" GroupName="ToothCondition" runat="server" />Sensitivity to percussion</label> 
       <label><asp:CheckBox ID="chkToothCondition06" GroupName="ToothCondition" runat="server" />Possible combined perio-endo lesion</label> 
       <label><asp:CheckBox ID="chkToothCondition07" GroupName="ToothCondition" runat="server" />Suspected cracked tooth/root</label> 
      </fieldset> 
     </li> 
    </ol> 
    <ol id="retreatment"> 
     <li> 
      <asp:Label ID="lblRetreatmentToothNumber" AssociatedControlID="txtRetreatmentToothNumber" runat="server">Re-treatment tooth number</asp:Label> 
      <asp:TextBox id="txtRetreatmentToothNumber" CssClass="textbox" runat="server" /> 
     </li> 
     <li> 
      <asp:Label ID="lblPreviousRCTDate" AssociatedControlID="txtPreviousRCTDate" runat="server">Date of previous RCT</asp:Label> 
      <asp:TextBox id="txtPreviousRCTDate" CssClass="textbox datepicker" runat="server" /> 
     </li> 
    </ol> 
    <ol id="treatment"> 
     <li> 
      <asp:Label ID="lblTreatmentToothNumber" AssociatedControlID="txtTreatmentToothNumber" runat="server">Treatment tooth number</asp:Label> 
      <asp:TextBox id="txtTreatmentToothNumber" CssClass="textbox" runat="server" /> 
     </li> 
     <li> 
      <fieldset> 
       <legend>What is the reason for treatment? (check any that apply)</legend> 
       <label><asp:CheckBox ID="chkTreatmentReason01" GroupName="TreatmentReason" runat="server" />Necessary for proper restoration</label> 
       <label><asp:CheckBox ID="chkTreatmentReason02" GroupName="TreatmentReason" runat="server" />Pulp exposed and vital</label> 
       <label><asp:CheckBox ID="chkTreatmentReason03" GroupName="TreatmentReason" runat="server" />Pulp exposed and non-vital</label> 
       <label><asp:CheckBox ID="chkTreatmentReason04" GroupName="TreatmentReason" runat="server" />Tooth was opened and medicated</label> 
       <label><asp:CheckBox ID="chkTreatmentReason05" GroupName="TreatmentReason" runat="server" />X-ray revealed radiolucency/pulpal involvement</label> 
      </fieldset> 
     </li> 
     <li> 
      <fieldset> 
       <legend>Is an X-ray included for revealed radiolucency/pulpal involvement?</legend> 
       <label><asp:RadioButton ID="rdoXrayIncluded01" GroupName="XrayIncluded" runat="server" />Yes</label> 
       <label><asp:RadioButton ID="rdoXrayIncluded02" GroupName="XrayIncluded" runat="server" />No</label> 
      </fieldset> 
     </li> 
    </ol> 
</li> 

非侵入式的JavaScript,我搶的形式ID(「referralform」),創建兩個數組包含相關的元素身份證,隱藏我想打開,用CSS類的元素,然後應用到應用,揭示了元素的CSS類onclick事件:

function prepareAccordion() 
{ 
if (document.getElementById && document.getElementsByTagName) 
{ 
    if (document.getElementById("referralform")) 
    { 
     var accordionids = new Array(); 
     accordionids[0] = document.getElementById("minorparent"); 
     accordionids[1] = document.getElementById("consultation"); 
     accordionids[2] = document.getElementById("retreatment"); 
     accordionids[3] = document.getElementById("treatment"); 

     var revealids = new Array(); 
     revealids[0] = document.getElementById("minorYes"); 
     revealids[1] = document.getElementById("minorNo"); 
     revealids[2] = document.getElementById("procedure01"); 
     revealids[3] = document.getElementById("procedure02"); 
     revealids[4] = document.getElementById("procedure03"); 

     for (var i = 0; i < accordionids.length; i++) 
     { 
      accordionids[i].className = "accordion-collapsed"; 
     } 

     revealids[0].onclick = function revealAccordion() 
     { 
      accordionids[0].className = "accordion-revealed"; 
     } 

     revealids[1].onclick = function revealAccordion() 
     { 
      accordionids[0].className = "accordion-collapsed"; 
     } 

     x = 0; 
     revealids[2].onclick = function revealAccordion() 
     { 
      if (x == 0) 
      { 
       accordionids[1].className = "accordion-revealed"; 
       x++; 
      } 
      else 
      { 
       accordionids[1].className = "accordion-collapsed"; 
       x--; 
      } 
     } 

     y = 0; 
     revealids[3].onclick = function revealAccordion() 
     { 
      if (y == 0) 
      { 
       accordionids[2].className = "accordion-revealed"; 
       y = 1; 
      } 
      else 
      { 
       accordionids[2].className = "accordion-collapsed"; 
       y = 0; 
      } 
     } 

     z = 0; 
     revealids[4].onclick = function revealAccordion() 
     { 
      if (z == 0) 
      { 
       accordionids[3].className = "accordion-revealed"; 
       z = 1; 
      } 
      else 
      { 
       accordionids[3].className = "accordion-collapsed"; 
       z = 0; 
      } 
     } 
    } 
} 
} 

function addLoadEvent(func) 
{ 
var oldonload = window.onload; 
if (typeof window.onload != 'function') 
{ 
    window.onload = func; 
} 
else 
{ 
    window.onload = function() 
    { 
     if (oldonload) 
     { 
      oldonload(); 
     } 
     func(); 
    } 
} 
} 

addLoadEvent(prepareAccordion); 

有可能是一個更好的方法來對打造」開/關「切換複選框的設置比使用」1「和」0「翻轉在他們之間,但是,我不是程序員。問題是,當在「1」和「0」之間應用翻轉時,onclick事件僅在直接在複選框內單擊時才起作用,而不在標籤上起作用。 onclick在應用於單選按鈕的標籤時工作正常,所以它必須是「1」和「0」之間的關鍵字,它會拋出一些東西。

任何幫助,這是非常感謝。

回答

0

我會做什麼—而事實上,我爲我的僱主網站—所做的工作是用類「toggleOnSwitch」(我傾向於使用刺激性的長名稱)標記頁面元素。該元素還將具有一些「data-」屬性:「data-switch」,其中包含用於控制切換的複選框或單選按鈕或<option>元素的「id」值; 「data-toggle-class」,包含可以打開和關閉的類(如「hidden」或「inactive」或其他);也許還有一兩個我忘記了。

不引人注意的JavaScript查找該類,然後爲每個切換的元素設置交換元素(複選框,在您的情況下)的事件處理程序。事件處理程序執行從切換元素中添加/刪除類的工作。

這是比這更復雜一點,如果你想切換的單選按鈕,因爲當一個單選按鈕開關,其所有的朋友開關關閉,所以處理程序必須知道在觸發切換處理所有其他單選按鈕具有相同的名稱,以防他們也控制頁面元素的可見性(或任何添加/刪除的類)。我也編寫了我的代碼,以便如果「數據開關」值以「!」開頭,例如「!toggleCheckbox」,則意味着反轉切換的意義。當一個特定的複選框在切換另一個事件的同時切換一件事情時,這很方便。