我怎样才能让textarea成为只读,但不会变成灰色

时间:2013-11-14 15:13:46

标签: javascript php

我怎样才能让textarea成为只读但不会变成灰色的?

我正在使用此脚本,但它不起作用:

$(".readonly").keydown(
function keydown(e) {
  if(!e.ctrlKey) return false; // Cancel non ctrl-modified keystrokes
  if(e.keyCode == 65) return true;// 65 = 'a'
  if(e.keyCode == 67) return true;// 67 = 'c'
  return false;
}
)

我尝试使用<textarea readonly>但是它使该字段变灰,我希望它只是readonly但不希望textarea字段变为灰色。

这是我的整个代码:

<body>

 <form name="search" method="post" action="">
 <fieldset style='width: 500px'>
 <legend align='left'><strong>Search</strong></legend>
 <p>
 Seach for: <input type="text" name="find" id="find" /> in 
 <Select NAME="field" id="field">
 <Option VALUE="testA">A</option>
 <Option VALUE="testB">B</option>
 <Option VALUE="testC">C</option>
 <Option VALUE="testD">D</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" id="search" value="Search" />
 </p>
 </fieldset>
 </form>

<script>
$(".readonly").keydown(
function keydown(e) {
  if(!e.ctrlKey) return false; // Cancel non ctrl-modified keystrokes
  if(e.keyCode == 65) return true;// 65 = 'a'
  if(e.keyCode == 67) return true;// 67 = 'c'
  return false;
}
)
</script>

<?php

 //This is only displayed if they have submitted the form 
if (isset($_POST['searching']) && $_POST['searching'] == "yes") 
{ 

//If they did not enter a search term we give them an error 
if (empty($_POST['find'])) 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

 // Otherwise we connect to our Database 
 mysql_connect("host", "username", "passw") or die(mysql_error()); 
 mysql_select_db("testdb") or die(mysql_error()); 

 // We preform a bit of filtering 

 $find = strtoupper($_POST['find']); 
 $find = strip_tags($_POST['find']); 
 $find = trim ($_POST['find']); 
 $field = trim ($_POST['field']);

 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM testtable WHERE UPPER($field) LIKE UPPER('%$find%')"); 


 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 $myRes = "<form action='' method='post'>
          <fieldset style='width: 10px'>
          <legend align='left'><strong>Results</strong></legend>
          <p>
          <table width='auto' border='1' cellspacing='1' cellpadding='1' align='center'>
          <tr>
          <th align='center' scope='row'>A</th>
          <td><textarea class=readonly name=testA id=testA cols=65 rows=3>" . $result['testA'] . "</textarea></td>
          </tr>
          <tr>
          <th scope='row'>B</th>
          <td><textarea class=readonly name=testB id=testB cols=65 rows=3>" . $result['testB'] . "</textarea></td>
          </tr>
          </table>
          </p>
          </fieldset>
          </form>";
 }
echo $myRes;  

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 

?>
</body>

1 个答案:

答案 0 :(得分:7)

你可以使用css

类似

textarea[readonly="readonly"], textarea[readonly] { //your styling }

例如

textarea[readonly="readonly"], textarea[readonly] { background-color:white; }

另请注意,mysql_函数已被弃用。您应该使用预先准备好的语句来使用MySQLi或PDO。

查看你的代码,你使用的是class = readonly,除非你真的在css中创建了那个你应该使用的类

<textarea readonly></textarea>