2012-04-06 89 views
1

這是在jQuery中傳遞兩個id的正確方法嗎?jQuery:多個值 - 下拉菜單

我遇到以下問題,如圖中所示。

域名註冊:不顯示輸入框。

域名轉移:只顯示一個輸入框。

圖片:

enter image description here

CSS:

#domainToBeReged, #domainToBeReged0, #domainToBeTransfered{ 
    display:none; 
} 

HTML:

<label for="domainRequired">Domain Registration: </label> 
        <select name="domainRequired" id="domainRequired"> 
         <option value="pleaseSelect">Please Select</option> 
         <option value="yes">Yes</option> 
         <option value="no">No</option> 
        </select> 
     <div id="domainToBeReged"> 
      <label for="domainToBeReged">Domain One:</label><input name="domainToBeReged" id="domainToBeReged" type="text" placeholder="http://www." /> 
      <label for="domainToBeReged0">Domain Two:</label><input name="domainToBeReged0" id="domainToBeReged0" type="text" placeholder="http://www." /> 

     </div> 
     <label for="domainTransfer">Domain Transfer: </label> 
        <select name="domainTransfer" id="domainTransfer"> 
         <option value="pleaseSelect">Please Select</option> 
         <option value="yes">Yes</option> 
         <option value="no">No</option> 
        </select> 
     <div id="domainToBeTransfered"> 
        <label for="domainToBeTransfered">Domain:</label><input name="domainToBeTransfered" id="domainToBeTransfered" type="text" placeholder="http://www." /> 
        <label for="domainToBeTransfered0">Domain:</label><input name="domainToBeTransfered0" id="domainToBeTransfered0" type="text" placeholder="http://www." /> 
     </div> 
     <label for="currentHosting">Current Hosting: </label> 
     <textarea cols="10" rows="10" name="currentHosting" id="currentHosting"></textarea> 
     <input type="submit" class="nextButton" value="Next" /> 
     </fieldset> 
    </form> 

jQuery的:

jQuery(document).ready(function() { 
$('select[name="domainRequired"]').change(function() { 
      var $domain = $('#domainToBeReged','#domainToBeReged0'); 
       if ($(this).val() == 'yes') { 
        $domain.show(); 
       } else { 
        $domain.hide(); 
     } 
      });    


     $('select[name="domainTransfer"]').change(function() { 
      var $domain = $('#domainToBeTransfered'); 
       if ($(this).val() == 'yes') { 
        $domain.show(); 
       } else { 
        $domain.hide(); 
     } 
      });    

    }); 

回答

4

這不完全正確的:

  • $("selector1", "selector2")是內部selector2尋找selector1。它基本上等於$("selector2").find("selector1")。你似乎只想要兩個獨立的選擇器。改爲使用逗號選擇器
  • 您可以使用.toggle(bool)來消除ifshow/hide
$("#domainToBeReged, #domainToBeReged0").toggle($(this).val() === "yes"); 
1

沒有你的jQuery必須是:

$('#domainToBeReged,#domainToBeReged0');