2013-03-05 70 views
1

我不知道如何使用選擇框選項啓用和禁用文本字段。啓用和禁用使用選擇框更改

也許我在下面的代碼中完全寫錯了。 如果我這樣做,請告訴我正確的代碼。

<body> 
<table width="500" border="1"> 
    <form style="text-align:center" id="form1" name="form1" method="post" action=""> 
    <tr> 
     <th width="32" nowrap="nowrap">Status</th> 
     <th width="32" nowrap="nowrap">Runs</th> 
     <th width="16" nowrap="nowrap">6</th> 
    </tr> 
<?php 
$('#Status1').change(function(){ 
var theVal = $('#Status1').val(); 
switch(theVal){ 
    case'0': 
     $('#Runs1').prop('disabled', true); 
     break; 
    case'1': 
     $('#Runs1').prop('disabled', false); 
     break; 
    case'2': 
     $('#Runs1').prop('disabled', true); 
     break; 
    } 
}); 
?> 
<tr> 
<td align='center'> 
    <select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td> 
<td align='center'> 
    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />  
</td> 
</tr> 
</form> 
</table> 
</body> 
+0

PHP ???? jQuery的! – 2013-03-05 10:23:35

+1

你是在開玩笑@PSR,而不是PHP標籤,它應該是腳本標籤,這個函數應該是$(document).ready函數。 – 2013-03-05 10:23:53

+0

哪裏包括你的JQuery文件? – 2013-03-05 10:24:10

回答

2

啓用/禁用#Run1輸入框的代碼嵌套在PHP標記(服務器端)之間,但是此代碼是用javascipt(客戶端)編寫的。在<head>

<body> 
<table width="500" border="1"> 
    <form style="text-align:center" id="form1" name="form1" method="post" action=""> 
    <tr> 
     <th width="32" nowrap="nowrap">Status</th> 
     <th width="32" nowrap="nowrap">Runs</th> 
     <th width="16" nowrap="nowrap">6</th> 
    </tr> 
<script type="text/javascript"> 
$('#Status1').change(function(){ 
var theVal = $('#Status1').val(); 
switch(theVal){ 
    case'0': 
     $('#Runs1').prop('disabled', true); 
     break; 
    case'1': 
     $('#Runs1').prop('disabled', false); 
     break; 
    case'2': 
     $('#Runs1').prop('disabled', true); 
     break; 
    } 
}); 
</script> 
<tr> 
<td align='center'> 
    <select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td> 
<td align='center'> 
    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />  
</td> 
</tr> 
</form> 
</table> 
+0

代碼將工作,但我寧願在表之前或表後使用腳本。一些html元素序列中的腳本看起來不太好。不好的做法.. – 2013-03-05 10:33:00

+0

@FaizulHasan:答案只修復現有的代碼。您的評論很棒,但在針對提問人員的主要問題上可能會更好。這是他們的代碼:) – Nope 2013-03-05 10:34:50

+0

@FrançoisWahl:謝謝..我的錯誤.. :) – 2013-03-05 10:37:21

1

更換<?php<script>?></script>.休息所有的東西是好的。

您在php標籤中寫的代碼是一個Jquery代碼,所以它應該在腳本標籤下。

+0

感謝Sankalp Mishra,但我想在php中這樣做是在PHP文件中工作也 – ShriD 2013-03-05 10:29:53

+0

是的。即使你將它保存爲帶有腳本標籤的.php文件,它也可以正常工作。 :) – 2013-03-05 10:30:41

+0

感謝它現在的所有工作 – ShriD 2013-03-05 11:08:46

1

負荷jQuery和添加的JavaScript那裏..不是內部<?php(PHP代碼)

<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 
$('#Status1').change(function(){ 
    var theVal = $('#Status1').val(); 
    switch(theVal){ 
    case'0': 
    $('#Runs1').prop('disabled', true); 
    break; 
    case'1': 
    $('#Runs1').prop('disabled', false); 
    break; 
    case'2': 
    $('#Runs1').prop('disabled', true); 
    break; 
} 
}); 
</script> 
<body> 
....//rest of your code 
+0

感謝它現在的工作 – ShriD 2013-03-05 11:11:58

1

您還沒有包括jQuery的文件和使用的標籤,而不是爲JavaScript代碼

<body> 
<table width="500" border="1"> 
<form style="text-align:center" id="form1" name="form1" method="post" action=""> 
<tr> 
    <th width="32" nowrap="nowrap">Status</th> 
    <th width="32" nowrap="nowrap">Runs</th> 
    <th width="16" nowrap="nowrap">6</th> 
</tr> 
<script src="../jquery.js"></script> 
<script type="text/javascript"> 
$('#Status1').change(function(){ 
var theVal = $('#Status1').val(); 
switch(theVal){ 
case'0': 
    $('#Runs1').prop('disabled', true); 
    break; 
case'1': 
    $('#Runs1').prop('disabled', false); 
    break; 
case'2': 
    $('#Runs1').prop('disabled', true); 
    break; 
    } 
}); 
</script> 
<tr> 
<td align='center'> 
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td> 
<td align='center'> 
    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />  
</td> 
</tr> 
</form> 
</table> 
</body> 
+0

感謝它現在的工作 – ShriD 2013-03-05 11:10:43

+0

Atleast接受答案或upvote – 2013-03-05 11:45:21

1

試試看喜歡:

HTML

<body> 
    <table width="500" border="1"> 
     <form style="text-align:center" id="form1" name="form1" method="post" action=""> 
     <tr> 
      <th width="32" nowrap="nowrap">Status</th> 
      <th width="32" nowrap="nowrap">Runs</th> 
      <th width="16" nowrap="nowrap">6</th> 
     </tr> 

     <tr> 
      <td align='center'> 
      <select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td> 
      <td align='center'> 
      <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />  
      </td> 
     </tr> 
    </form> 
    </table> 
</body> 

SCRIPT

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#Status1').change(function(){ 
     var theVal = $('#Status1').val(); 
     switch(theVal){ 
      case'0': 
       $('#Runs1').prop('disabled', true); 
       break; 
      case'1': 
       $('#Runs1').prop('disabled', false); 
       break; 
      case'2': 
       $('#Runs1').prop('disabled', true); 
       break; 
     } 
    }); 
}); 
</script> 

但首先應該檢查你在head section增加了jquery library file

+0

感謝現在所有的工作 – ShriD 2013-03-05 11:11:32

0

朋友這是我的問題的解決方案,在你們的幫助下,謝謝。

<html> 
<head> 
<script type="text/javascript" src="../jquery.min.js"></script> 
<SCRIPT LANGUAGE="JavaScript"> 
$(document).ready(function(){ 
    $('#Status1').change(function(){ 
     var theVal = $('#Status1').val(); 
     switch(theVal){ 
      case'0': 
       $('#Runs1').prop('disabled', true); 
       break; 
      case'1': 
       $('#Runs1').prop('disabled', false); 
       break; 
      case'2': 
       $('#Runs1').prop('disabled', true); 
       break; 
     } 
    }); 
}); 
</script> 
</head> 
<body> 
    <table width="500" border="1"> 
     <form style="text-align:center" id="form1" name="form1" method="post" action=""> 
     <tr> 
      <th width="32" nowrap="nowrap">Status</th> 
      <th width="32" nowrap="nowrap">Runs</th> 
      <th width="16" nowrap="nowrap">6</th> 
     </tr> 
     <tr> 
      <td align='center'> 
      <select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td> 
      <td align='center'> 
      <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />  
      </td> 
     </tr> 
    </form> 
    </table> 
</body> 
</html>