2013-10-03 27 views
0

啓用下拉菜單我在<table>一個下拉框:上檢查複選框

<TR > 
    <TD colspan="2" class="text1"> 
     <div align="right"> 
      <FONT color="#000000"> 
       <INPUT name="services[]" type="checkbox" id="services[]" value="Employee "> 
      </FONT> 
     </div> 
    </TD> 

    <TD width="321" align="center" valign="top" class="text1"> 
     <DIV align="left">Employee</DIV> 
    </TD> 
</TR> 

當用戶選擇上述複選框我要啓用以下下拉菜單:

<TR> 
    <TD width="155" height="25" align="right" class="text1"> 
     <div align="right">Enquiry Type/For</div> 
    </TD> 

    <TD align="left" colspan="2" height="25">&nbsp; 
     <select name = "type" id = "type"> 
      <!--<option value = "general">General</option>--> 
      <option value = "general">General</option> 
      <option value = "employee">employee</option> 
      <option value = "student">student</option> 
     </select> 
    </TD> 
</TR> 

我該怎麼做?

小提琴Here

+0

你可以在第一個下拉框的onchange上使用jQuery來調用ajax頁面。 – Naruto

回答

2

首先,改變你的複選框id來services

<INPUT name="services[]" type="checkbox" id="services" value="Employee " rel="services"> 

然後更改您的下拉:

<select name = "type" id = "type" disabled="disabled"> 

現在,使用jQuery:

$(function(){ 
$("[rel=services]").click(function(){ 
    $("#type").attr("disabled", false); 
}); 
}); 

注意:type是反轉關鍵字,所以請將name更改爲其他內容。

+0

我必須保持服務[],因爲有許多複選框的值和每個文本框的ID是作爲數組「服務[]」給出的,所以我如何修改您給的JavaScript以適應我的代碼需求? – user2822187

+0

好的,給你發送更新。 – Pupil

+0

現在查看更新後的答案。 – Pupil

0

試試這個,我覺得你喜歡這個

$('input[type=checkbox]').change(function() 
{ 
    if($(this).prop('checked')) 
    $('#type').show(); 
    else 
     $('#type').hide(); 
}); 

小提琴Here

+0

這很好,但我有很多複選框。這就是爲什麼我已爲每個文本框提供id爲「services []」。我如何使用您的代碼來滿足我的編碼需求?您提供的解決方案很好,但我想知道如何修改我的代碼 – user2822187

+0

如果有多個複選框,對於每個複選框,它都會調用該函數。 – Pupil

+0

@編程學生: 我的意思是 - 是有很多checkox,但我希望只有在選中此特定複選框時才能啓用下拉列表 – user2822187

0

沒有jQuery的解決方案:

document.getElementById('services').onchange = function() { 
    document.getElementById('type').disabled = !document.getElementById('type').disabled; 
}; 

通知我改變了複選框,ID爲 '服務',並添加禁用到選擇:

<select name = "type" id = "type" disabled> 
+0

我有很多複選框,id = services []。只有當一個特定的複選框被選中時,才應該啓用下拉菜單 – user2822187

+0

你不應該在頁面上有多於1個元素的相同ID。 – shem86

+0

是的,我知道但假設如果我使用服務[]是否像數組一樣工作? 我實際上是在修改以前的人編寫的代碼。我只需要修改它以啓用下拉 – user2822187

0

這裏是您的解決方案

page1.php中

<table id='mytable'> 
<TR > 
    <TD colspan="2" class="text1"> 
     <div align="right"> 
      <FONT color="#000000"> 
       <INPUT name="services[]" type="checkbox" id="services[]" value="Employee "> 
      </FONT> 
     </div> 
    </TD> 

    <TD width="321" align="center" valign="top" class="text1"> 
     <DIV align="left">Employee</DIV> 
    </TD> 
</TR> 
</table> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script> 
<script> 
$(document).ready(function(e) { 
    $('input[name="services[]"]').change(function(){ 
     if(!$(this).is(":checked")) { 
      $(".newRow").remove(); 
     } else { 
      $.ajax({ 
       url:"page2.php", 
       success: function(response) { 
        $("#mytable").append(response); 
       } 
      }); 
     } 
    }); 
}); 
</script> 

使page2.php

<TR class="newRow"> 
    <TD width="155" height="25" align="right" class="text1"> 
     <div align="right">Enquiry Type/For</div> 
    </TD> 

    <TD align="left" colspan="2" height="25">&nbsp; 
     <select name = "type" id = "type"> 
      <!--<option value = "general">General</option>--> 
      <option value = "general">General</option> 
      <option value = "employee">employee</option> 
      <option value = "student">student</option> 
     </select> 
    </TD> 
</TR> 
1

你可以根據下面的代碼更改腳本.....

function check() { 

if (($("#check:checked").length) > 0) { 
    $("#type").removeAttr("disabled"); 
} else { 
    $("#type").attr("disabled", "disabled"); 

} 
} 
用於演示的

JSFIDDLE