2011-04-21 30 views
2

工作,我有這樣的JavaScript代碼:<select> jQuery的點擊不IE7,IE8和Safari

<script type="text/javascript"> 
    $(document).ready(function() { 

     $("#no-option").click(function() { 
      $("#additional-info").html(''); 
     }, 
     function() {} 
     );  
     $("#military-option").click(function() { 
      $("#additional-info").append('<label>Current Employer:</label><input type="text" name="currentemployer" class="textfield" /><br /><label>Role:</label><input type="text" name="role" class="textfield" /><br /><label>Total Hours:</label><input type="text" name="totalhours" class="textfield" /><br />'); 
     }, 
     function() {} 
     ); 
     $("#corporate-option").click(function() { 
      $("#additional-info").append('<label>Current Employer:</label><input type="text" name="currentemployer" class="textfield" /><br /><label>Role:</label><input type="text" name="role" class="textfield" /><br /><label>Total Hours:</label><input type="text" name="totalhours" class="textfield" /><br />'); 
     }, 
     function() {} 
     ); 
     $("#uav-option").click(function() { 
      $("#additional-info").append('<label>Write a brief description of UAV Experience:</label><textarea name="briefdescription" cols="" rows="" class="textarea"></textarea>'); 
     }, 
     function() {} 
     ); 

    }); 
</script> 

和HTML部分:

<select name="interest" class="validate[required] dropdown"> 
    <option value="" id="no-option" selected="selected">- Please Select -</option> 
    <option value="Military" id="military-option">Military</option> 
    <option value="Private/Corporate" id="corporate-option">Private/Corporate</option> 
    <option value="UAV" id="uav-option">UAV</option> 
</select> 

這在FF和IE9可以正常使用。有人可以幫我解決這個問題的IE7/8?

回答

5

不可能。舊版IE版本無法識別選項點擊事件。您將不得不使用select change事件。

e.g

$('#interest').change(selectChange); 

function selectChange(){ 

    var selectedValue = $(this).val(); 

    switch (selectedValue){ 

    case 'Military': 
     //do something 
     break; 

    case 'Private/Corporate': 
     //do something else 
     break; 

    } 

} 
+0

你能告訴我,我該怎麼辦呢?一個例子? – Andrei 2011-04-21 09:26:57

+0

@Andrei - 上面的快速示例 – redsquare 2011-04-21 09:31:03

1

IE7/8不會與選項 「單擊」 事件正常工作。您可以使用「改變」事件是這樣的:

$(document).ready(function() { 
    $("#interest").change(function(){ 
     switch ($(this).val()){ 
      case 'Military': 
       $("#additional-info").append('<label>Current Employer:</label><input type="text" name="currentemployer" class="textfield" /><br /><label>Role:</label><input type="text" name="role" class="textfield" /><br /><label>Total Hours:</label><input type="text" name="totalhours" class="textfield" /><br />'); 
       break; 
      case 'Private/Corporate': 
       $("#additional-info").append('<label>Current Employer:</label><input type="text" name="currentemployer" class="textfield" /><br /><label>Role:</label><input type="text" name="role" class="textfield" /><br /><label>Total Hours:</label><input type="text" name="totalhours" class="textfield" /><br />'); 
       break; 
      case 'UAV': 
       $("#additional-info").append('<label>Write a brief description of UAV Experience:</label><textarea name="briefdescription" cols="" rows="" class="textarea"></textarea>'); 
       break; 
     } 
    });          
}); 

你可以在這裏進行測試 http://jsfiddle.net/sWEdd/4/