1

我怎样才能使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>,但它使该字段变灰,我希望它是只读的,但不希望 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>
4

1 回答 1

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>
于 2013-11-14T15:15:02.370 回答