javascript - how can i make textarea a read only but will not be greyed out - Stack Overflow

how can i make textarea a read only but will not be greyed out?i am using this script but its not worki

how can i make textarea a read only but will not be greyed out?

i am using this script but its not working:

$(".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;
}
)

i tried to use <textarea readonly> but its making the field greyed out, I want it to be readonly but dont want the textarea field to change to being greyed out.

here is my entire code:

<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>

how can i make textarea a read only but will not be greyed out?

i am using this script but its not working:

$(".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;
}
)

i tried to use <textarea readonly> but its making the field greyed out, I want it to be readonly but dont want the textarea field to change to being greyed out.

here is my entire code:

<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>
Share Improve this question asked Nov 14, 2013 at 15:13 user2579439user2579439 1,2433 gold badges14 silver badges13 bronze badges 4
  • The greyed out part sounds like you are using the attribute disabled instead of the property readonly jsfiddle/8Vss5 – lt.kraken Commented Nov 14, 2013 at 15:18
  • @larssy1 no im not using disabled, however you called the color...i dont want the textarea changing its color when im using readonly – user2579439 Commented Nov 14, 2013 at 15:21
  • Which I don't understand. Are you sure none of your nor external stylesheets effect a textarea with that attribute? – lt.kraken Commented Nov 14, 2013 at 15:22
  • @larssy1 this is just the content of my stylesheet as advised by Fredd: @charset "utf-8"; /* CSS Document */ textarea[readonly="readonly"] { //style } – user2579439 Commented Nov 14, 2013 at 15:28
Add a ment  | 

1 Answer 1

Reset to default 7

You could use css

something like

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

eg

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

Also note that the mysql_ functions have been deprecated. You should MySQLi or PDO with prepared statements.

Looking at your code you are using class=readonly, unless you've actually created that class in css you should be using

<textarea readonly></textarea>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744265243a4565835.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信