【问题标题】:Sava html attr in php variablephp变量中的Sava html attr
【发布时间】:2018-11-23 22:26:24
【问题描述】:

我有这样的清单

John
   Jack (link)
       Husam (link)
   Koko (link)
       Rami (link)
       Loay (link)

并且我有一个包含上述所有名称的下拉列表,当我单击 Husam 侧的链接时,我想在下拉列表中将他的父级 (Jack) 显示为选定的选项,因此我需要将其孩子的 ID 传递给函数 getParentId链接点击了,这个 id 在里面,所以我如何将 attr 名称传递给 $_GET['childId'] 而不是 $_GET['childId'] = 4。 我尝试将链接的attr 保存在 php 变量中。

有没有可能?

这是我在index.php中的代码

$_GET['childId'] = 4; // here i don't want to pass 4 i need to pass $(a).attr('name'); from another page
if ($object->getParentId($_GET['childId'])) {
  echo "<script>
  function changeParent(){
    $(document).ready(function(){
      $('a').on('click',function() {
        var x = $(this).attr('id');
        var y = $(this).attr('name');
        $.ajax({
          type: 'POST',
          url: 'http://test.local/Family.php?action=getId',
          data: {'childId' : $_GET[childId]},
          success: function(msg) {
            document.getElementById('names').value = x;
            $('#save').show();
          }
        });
      });
    });
  }
  </script>";
}

这是我想在Family.php(a href 的名称)中传递的内容

function getChild($family_tree,$parent){
$list = "<ul class='listSet' style='list-style-type:none'>";
foreach($family_tree[$parent] as $each_child) {
  $list .= "<li>" . $each_child[0]."  "."<a  onclick='changeParent()' id='$parent' name='$each_child[1]' href='#'>".'Change parent'."</a>";
  if(isset($family_tree[$each_child[1]])){...

【问题讨论】:

  • 您在oreach( 没有收到错误消息吗?
  • 对不起,我编辑了……复制过去的错误
  • 我应该传入 getParentId($_GET['childId'])) HTML 中一些 attr 的值我该怎么做?如果您想详细描述我的情况,我可以。 :D @PhilippMaurer
  • @PhilippMaurer 我编辑了我的描述请检查一下。

标签: javascript php jquery html dom


【解决方案1】:

好的,为了做到这一点,您必须对功能进行一些更改。

这个条件if ($object-&gt;getParentId($_GET['childId']))你应该从index.php移动到Family.php

$('a').on('click',function() { 中的下一行将data: {'childId' : $_GET[childId]}, 这一行更改为data: {'childId' : y},

function changeParent(){
  $(document).ready(function(){
    $('a').on('click',function() {
      var x = $(this).attr('id');
      var y = $(this).attr('name');
      $.ajax({
        type: 'POST',
        url: 'http://test.local/Family.php?action=getId',
        data: {'childId' : y},
        success: function(msg) {
          document.getElementById('names').value = x;
          $('#save').show();
        }
      });
    });
  });
}

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 2017-03-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多