2014-12-19 51 views
0

我正在創建選擇框,並且我爲jquery.it中的選擇框編寫了更改函數。但我的問題是,當我在選擇框中移動光標選項時,沒有選擇任何選項,我點擊了選擇框旁邊。但改變功能調用我不知道如何解決這個問題我的代碼被賦予波紋管jQuery中的onchange問​​題

和我的小提琴被http://jsfiddle.net/6mhed8yL/

<select name="selecting" id="selecting" > 
    <option selected="selected" >--Control--</option> 
    <option id="Select All ">Select All</option> 
    <option id="Remove All " >Remove All</option> 
    <option id="Sample Questions" >Sample Questions</option> 
    <option id=" User Added Questions">User Added Questions</option> 
    <option id="Show All Questions" >Show All Questions</option> 
       </select> 

jQuery的

$('#selecting').change(function(){ 
     alert("check"); 
     var opt=$('#selecting').val(); 
     var url=""; 
     alert("option is"+opt); 
     // $("#selecting option:selected").removeAttr('disabled'); 
      //$("#selecting option:disabled").val(opt); 
      //$("#selecting option[value=" + opt + "]").attr('disabled', 'disabled'); 


     if(opt=="Select All") 
      { 
      $('input:checkbox').prop('checked', true); 
      $("select option:contains('Select All')").attr("disabled","disabled"); 
      $("select option:contains('Remove All')").removeAttr("disabled"); 
      $("select option:contains('Sample Questions')").removeAttr("disabled"); 
      $("select option:contains('User Added Questions')").removeAttr("disabled"); 
      $("select option:contains('Show All Questions')").removeAttr("disabled"); 


      } 
     else if(opt=="Remove All") 
      { 
      $("input:checkbox").prop("checked", false); 
      $("select option:contains('Remove All')").attr("disabled","disabled"); 
      $("select option:contains('Select All')").removeAttr("disabled"); 
      $("select option:contains('Sample Questions')").removeAttr("disabled"); 
      $("select option:contains('User Added Questions')").removeAttr("disabled"); 
      $("select option:contains('Show All Questions')").removeAttr("disabled"); 
      } 
     else if(opt=="Sample Questions") 
      { 

      $("select option:contains('Sample Questions')").attr("disabled","disabled"); 
      $("select option:contains('Select All')").removeAttr("disabled"); 
      $("select option:contains('Remove All')").removeAttr("disabled"); 
      $("select option:contains('User Added Questions')").removeAttr("disabled"); 
      $("select option:contains('Show All Questions')").removeAttr("disabled"); 
      url = "SampleQuestions.jsp"; 
      alert("sample"); 

      /* if (window.XMLHttpRequest) 
        req = new XMLHttpRequest(); 
        else if (window.ActiveXObject) 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        req.open("GET", url, true); 

        req.onreadystatechange =callback; 

        req.send(null); */ 



      } 
     else if(opt=="User Added Questions") 
     { 
      $("select option:contains('User Added Questions')").attr("disabled","disabled"); 
      $("select option:contains('Select All')").removeAttr("disabled"); 
      $("select option:contains('Remove All')").removeAttr("disabled"); 
      $("select option:contains('Sample Questions')").removeAttr("disabled"); 
      $("select option:contains('Show All Questions')").removeAttr("disabled"); 
      $("select option:contains('Show All Questions')").removeAttr("disabled"); 

      url="UserAddedQuestion.jsp"; 
      alert("user"); 

     /* if (window.XMLHttpRequest) 
        req = new XMLHttpRequest(); 
        else if (window.ActiveXObject) 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        req.open("GET", url, true); 

        req.onreadystatechange =callback; 

        req.send(null); 
        */ 

     } 
     else if(opt=="Show All Questions") 
      { 

      $("select option:contains('Show All Questions')").attr("disabled","disabled"); 
      $("select option:contains('Select All')").removeAttr("disabled"); 
      $("select option:contains('Remove All')").removeAttr("disabled"); 
      $("select option:contains('Sample Questions')").removeAttr("disabled"); 
      $("select option:contains('User Added Questions')").removeAttr("disabled"); 
      url="ShowAllQuestions.jsp"; 
      alert("all"); 

      /* if (window.XMLHttpRequest) 
        req = new XMLHttpRequest(); 
        else if (window.ActiveXObject) 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        req.open("GET", url, true); 

        req.onreadystatechange =callback; 

        req.send(null); 
        */ 

      } 



    }); 
+0

你的提琴

檢查需要選擇選項'沒有包裝 - 在' – Neverever 2014-12-19 06:25:33

+0

你描述的不是發生在我什麼,你的原代碼應該可以正常工作。 – Neverever 2014-12-19 06:27:02

+0

所有那些重複代碼肯定會減少很多 – charlietfl 2014-12-19 06:27:49

回答

0

我已經更新的jsfiddle現在的工作。這裏[fiddle][1]http://jsfiddle.net/6mhed8yL/1/

+0

第一次點擊出側的選擇框它沒有調用該功能,但在雙擊它調用function.check你的小提琴更新 – KVK 2014-12-19 06:30:26

+0

刪除警報和檢查,它調用函數第一次 – 2014-12-19 06:41:17

+0

我的問題是os我使用的是Ubuntu 14.0。但在相同的代碼在Windows中工作正常。我如何解決它在Ubuntu的 – KVK 2014-12-19 06:48:51