Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将光标位置重置为0 PHP Symfony 3.4_Php_Jquery_Ajax - Fatal编程技术网

将光标位置重置为0 PHP Symfony 3.4

将光标位置重置为0 PHP Symfony 3.4,php,jquery,ajax,Php,Jquery,Ajax,由于“$request->get('pos')”语句,我能够解决索引问题“pos” 我能够在ajax查询中恢复正确的光标位置,但在控制器中它总是重置为0。我不知道这个问题从何而来以及如何解决它,因为这是我第一次使用ajax。 我真的不明白它是怎么工作的 谢谢你的理解! 这是我的新剧本: 这是我的模板:new.html.twig {% extends '::layoutv.html.twig' %} <script type="text/javascript"> fu

由于“$request->get('pos')”语句,我能够解决索引问题“pos” 我能够在ajax查询中恢复正确的光标位置,但在控制器中它总是重置为0。我不知道这个问题从何而来以及如何解决它,因为这是我第一次使用ajax。 我真的不明白它是怎么工作的 谢谢你的理解! 这是我的新剧本: 这是我的模板:new.html.twig

{% extends '::layoutv.html.twig' %}
  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
{%块样式表%} {%endblock%}

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
{%block titre%} {%endblock%}

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
{%block body%}

<div>
    <h1>
        Insertion des projets
    </h1>
</div>
<!-- BEGIN DISPLAY THE FLASH MESSAGES -->
{% for flashMessage in app.session.flashbag.get('error') %}
    <div class="alert alert-error">
        <button class="close" data-dismiss="alert"></button>
        <strong><i class='icon-remove'></i> </strong> {{ flashMessage }}
    </div>
{% endfor %}
<!--  END DISPLAY THE FLASH MESSAGES -->

<div class="row-fluid">
    <div class="span6">


        <div class="portlet-body form">
            <!-- BEGIN FORM-->
            <form id="formProject" class="form-horizontal"
                  action="{{ path('new_project') }}" enctype="multipart/form-data" method="post" }>
                <div class="control-group">
                    <label class="control-label">Télécharger une image</label>
                    <div class="controls">
                        <input type="file" name="image">
                        </br>
                        <div class="row">
                            <div class="col-sm-8"  onClick="showWord()">
                                {{ form_row(formProject.texte|raw) }}
                            </div>
                        </div>

                    </div>
                    </br>
                </div>
                <div class="controls margin-bottom-30">
                    <button type="submit" name="submit" class="btn green">Valider</button>
                    <button type="reset" class="btn yellow">Annuler</button>
                </div>
                {{ form_rest(formProject) }}
            </form>
            <!-- END FORM-->
        </div>

    </div>
</div>
  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>

因为您没有告诉我们您使用的是什么PHP框架,所以在普通PHP中,您应该拥有变量的POST数据

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
$\u POST['pos'] 或 $\u请求['pos']

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
但是在Javascript代码中,您正在打印ajax成功的开始,也就是在值被发送到服务器并返回之后。。。因此,由于存在明显的延迟,这意味着在ajax启动之前,start变量可能为null

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
尝试在此行之后使用console.log(开始):

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>
start=cursorPos.selectionStart; console.log(启动)

  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>

在创建ajax之前,因为如果在该点为null,则服务器上会得到null。

console.log(start)?console.log(start)的输出是什么?console.log(start)返回一个整数,该整数是您尝试过
$\u get['pos']
?后端是否使用任何框架,例如,拉雷维尔?如果没有,我们能否获得有关
$request
及其
get
方法的一些信息?是的,我的意思是
$\u POST['pos']
我遵循了您告诉我的内容,但我遇到了相同的问题,它总是被重新初始化为0。那么如何解决这个问题呢?谢谢!
  <script type="text/javascript">
      function showWord() {
          cursorPos = document.getElementById("formation_minipbundle_project_texte");
          start = cursorPos.selectionStart;
          $.ajax({
              type: "POST",
              async: false,
              data:{'pos' : start},
              url: "{{ path('new_project') }}",
              success: function() {
                  console.log(start);
              }
          });

      }
  </script>