douliao8402 2019-07-17 00:09
浏览 55

在textarea中的多行上执行更新功能

I have a textarea that posts each line to a database and then pulls that data to display it on a separate text area. I have a function that when called calls a php update function. This updates the line in the database corresponding to the line in the textarea. What I want to do is when this update function is called, I want all the lines to be updated, not just the one that is selected. This is because at the moment I have to manually select each line to update if a change is made anywhere to enusre the lines and indentation are the same.

    <script>
    var lineNo = 0;
    var lineText = "";
    var ta;
    var numOfSpaces;


   function update(e) {
       ta = $("#textEditor")[0];
     lineNo = ta.value.substr(0, ta.selectionStart).split(/?
|/).length;
      lineText = ta.value.split(/?
|/)[lineNo - 1];
       numOfSpaces = lineText.split(/\s/).length - 1;


      console.log(" line num: " +lineNo+ " line data: " + lineText);



        $.ajax({

         url: "update1.php",
         method: "POST",
         data: {lineN: lineNo, lineT: lineText},
         dataType: 'text',
         success: function(data){

          console.log(data);
         }
       });
     }

           $('#textarea').keydown(update).mousedown(update);

The above is the function which catches the text at a line and the corresponding line number. It passes the information to the following php file

    <?php


    $linec = $_POST['lineT'];
    $linenumber = $_POST['lineN']



     $update = "UPDATE Code_Stream1 SET Line_Code='$linec' where LineNumber='$linenumber' ";

    $resultinsert = $conn->query($update);

    echo $update;


?>

ps I have a connection to the database but didn't include above

  • 写回答

1条回答 默认 最新

  • douyabu1528 2019-07-17 01:14
    关注

    Maybe I misunderstand your question, but couldn't you simply use .setSelectionRange on the textarea to select all text?

    $('#textEditor').on('focus', function(e) {
        // setTimeout skips to the next tik in the eventLoop
        // preventing timing issues in some browser
        setTimeout(function() {
            e.currentTarget.setSelectionRange(0, e.currentTarget.value.length);
        }, 0);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <textarea id="textEditor" rows="4">
        Line 1
        Line 2
        Line 3
        Line 4
    </textarea>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?