用新行设置文本区域的值


Setting the values of textarea with new lines

我有这段代码。我只想点击辅助按钮。数据库中的数据将显示在文本框和文本区域中。但我在文本区遇到了问题。在数据库中,它有新行,我无法得到它的值。有人能帮我吗??

<?php 
$tmp=mysqli_query($link, "select * from med_rec_tmp");
if($tmp){
while($row=mysqli_fetch_array($tmp)){
$rem = nl2br($row['Remarks']);?>
<tr>
<td><?php echo $row['Temp_RecNo']; ?></td>
<td><?php echo $row['FirstName']; ?></td>                                       
<td><?php echo $row['MiddleName']; ?></td>                                      
<td><?php echo $row['LastName']; ?></td>                                        
<td><?php echo $rem;?></td>
<td>                                                                            
<script type="text/javascript">                                                                     
$("#assist<?php echo $row['Temp_RecNo']; ?>").on('click', function(){
var txt = '<?php echo $rem; ?>';
var regex = /<br's*['/]?>/gi;
var txt2 = txt.replace(regex, "'n");
$("#rem").val(txt2);
$("#fname").val('<?php echo $row['FirstName'];?>');
$("#mname").val('<?php echo $row['MiddleName'];?>');
$("#lname").val('<?php echo $row['LastName'];?>');                                  
$("#id").val('<?php echo $row['Temp_RecNo'];?>');
});
</script>
<center>
<div class="btn-group btn-group-xs btn-group-justify" role="group" aria-label="...">                                                                            
<button type="submit" class='btn btn-info' id='assist<?php echo $row['Temp_RecNo']; ?>'><span class='fa fa-trash'></span>&nbsp;Assist</button>                                                                              
</div>                                                                              
</center>
</td>
</tr>
<?php }}?>

这是文本区域代码

<div class="col-lg-12" style="padding:0px;">
<label class="control-label">Remarks:</label>
</div>
<div class="col-lg-12" style="padding:0px;">
<textarea class="form-control" style="width:100%;max-width:100%;min-width:100%;min-height:80px;max-height:80px;resize:none;" id="rem" name="remarks" rows="10" cols="50"></textarea>
</div>

你的问题很模糊,但听起来你只需要文本区域来显示它的值,对吗?如果是,请使用以下代码:

<div class="col-lg-12" style="padding:0px;">
    <textarea class="form-control" style="width:100%;max-width:100%;min-width:100%;min-height:80px;max-height:80px;resize:none;" id="rem" name="remarks" rows="10" cols="50">
       <?php echo $row['textarea']; ?> //If you are trying to insert the value for 
                                       //textarea, this is how you do it.
     </textarea>
</div>

如果是其他事情,请更具体一些。

首先,我只想让您明白,在PHP代码中包含显示元素通常不是一种经过深思熟虑的做法。有很多模板引擎,相当多是基于Smarty的(谷歌是你的朋友)。这里有一个问题,它有一些很好的、更详细的答案,关于使用静态HTML的模板引擎,并让它们处理数据到视图的转换。既然我不知道你的经验水平,不妨抬头看看"MVC";或";MVC体系结构"-这是一套很好的规则,可以让你意识到,将每个文件的目标分开可以对你有很大帮助

同样,如果这就是代码在实际文件中的样子,没有缩进,你可能应该换一个文本编辑器,因为代码的可读性至关重要,如果你的编辑器不能帮助你保持所写内容的适度可读性,那肯定会伤害你。这是我发现的一篇随机文章,讨论了为什么缩进代码是你能做的最好的事情之一,不仅对你自己,而且对以后可能需要阅读文件的其他人来说。有很多选择,目前我正在使用Brackets,但你目前使用的一个很好的升级可能是Notepad++,我多年前就使用过它。

现在,为了(希望)回答你的问题(因为我没有数据可以处理),你的问题似乎是这样的:

var txt = '<?php echo $rem; ?>';

我假设你的字符串可能包含某种单引号,例如$rem的值类似于I don't think you should go to the meeting tomorrow as...,所以当代码从浏览器中打印出来时会发生这样的情况:

var txt = 'I don't think you should go to the meeting tomorrow as...';

甚至Stackoverflow着色也显示了这里的问题:txt被分配了I don的值,然后javascript在之后字符串其余部分的混乱文本中抛出错误。

我能看到你应用的最快的解决方案是:

var txt = '<?php echo addslashes($rem); ?>';

这将导致var text = 'I don''t think you should go to the meeting tomorrow as...';的输出,并且应该正常工作。

如果你只想从数据库中复制粘贴一行的数据,这样我就可以第一手了解问题的类型,我可以在回答时提供更多帮助,并在完成时编辑我提出的解决方案。

回顾

  1. 尝试开始将程序逻辑(控制器)与显示的HTML(视图/模型)分离
  2. 如果你的文本编辑器不能帮助你读写可读的代码,那就找一个更好的
  3. 试试我的快速解决方案,如果它不起作用(或者其他人还没有回答),请复制/粘贴一行数据供我(和其他人)使用,以进一步回答这个问题

我认为您的问题只是您的文本中有<br>标记,并且您希望这些标记在textarea中显示为换行符。如果这是正确的,这应该适用于您,只需将<br>'n替换为Carage返回&#13;&#10;的htmlentities,并将它们设置为textareahtml属性,而不是value

var txt2='this is a string<br>broken over several<br>lines';
$("#rem").html(txt2);
$("#rem").html( $("#rem").val().replace(/<br>|''n/ig, "&#13;&#10;") );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Add text with line breaks to textarea</h3>
<textarea cols='60' rows='8' id="rem"></textarea>