2017-06-01 69 views
0

相关我刚才的问题: How to use javascript load function twice? (load in load)来自PHP的foreach的Javascript数据。

我就拥有了一切工作(负荷负荷)。但是现在我想使用一个将数据发送到第二个负载的foreach。

当我使用这个foreach时,数据(POST ID)保持不变。

示例代码:

<script > 
    $(document).ready(function() { 
     // Forum categories 
     $("#main").on("click", "#message", function() { 
      $("#getmessage").load("forum/getmessage.php", {'id': $("#id").val()}); 
     }); 
    }); 
</script > 

<?php foreach($stickies as $sticky): ?> 
       <form id="form_id" > 
        <a href="#" class="list-group-item small" id="message" 
         style="background-color:black; color:white;" > 
         <span class="glyphicon glyphicon-pushpin" aria-hidden="true" ></span > 
         | <?php echo $sticky['header']; ?> 
        </a > 

        <input type="hidden" id="id" name="id" value="<?php echo $sticky['id']; ?>" > 

       </form > 
      <?php endforeach; ?> 

正如你可以看到我想推值ID作为POST数据,所以加载的文件可以在此POST ID执行查询。

如何为每个foreach发送正确的ID?

+3

首先了解元素__的id必须唯一__ –

回答

2

有很多方法来实现你所需要的。所以这只是一个例子。

首先 - id属性的值必须是唯一的。没有例外。这就是为什么id="message"每和id="id"input无效。对于类似的元素 - 使用类。

固定码将是:

<?php foreach($stickies as $sticky): ?> 
    <form> 
     <a href="#" class="list-group-item small js-message" 
      style="background-color:black; color:white;" > 
      <span class="glyphicon glyphicon-pushpin" aria-hidden="true" ></span > 
      | <?php echo $sticky['header']; ?> 
     </a > 
     <input type="hidden" name="id" value="<?php echo $sticky['id']; ?>" > 
    </form > 
<?php endforeach; ?> 

正如你所看到的,我删除了具有相同的值的id属性。对于input s和form s你甚至不需要它们,因为a s我增加了类js-message

接下来是处理程序:

$(document).ready(function() { 
    // Forum categories 
    $("#main").on("click", ".js-message", function() { 
     // ways of getting value id can be different. 
     $("#getmessage").load(
      "forum/getmessage.php", 
      {'id': $(this).parent().find("input").val() 
     }); 

     // optionally you can add 
     return false; 
     // to prevent default click action which in case 
     // of link is scrolling to the top of a page 
    }); 
}); 

在这里,作为的方式之一,以获取价值我用

$(this).parent().find("input").val() 

是:

$(this).parent() // find a parent of a clicked a, it will be `form` 
    .find("input") // among `form` children find `input` 
    .val()   // take value from found input 

当然,如果你的标记会更改和更多inputs将出现在窗体上,这可能会停止工作。但这仅仅是一个例子,你可以根据需要修改它。

+0

这工作!非常感谢你,u_mulder。我将其标记为答案 –

1

您正在使用id="id"创建很多元素,这是不正确的。 id属性在页面中必须是唯一的,因此您的jQuery只返回使用$("#id").val()时找到的第一个值。

而不是添加一个数据属性到按钮,可以像这样的点击事件检索,注意我也删除了id="message",并添加了一个消息类为上述原因,并删除表单元素,因为它不是需要:

<?php foreach($stickies as $sticky): ?> 
    <a href="#" class="list-group-item small message" 
     style="background-color:black; color:white;" data-id="<?php echo $sticky['id'] ?>"> 
     <span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span> | <?php echo $sticky['header']; ?> 
    </a> 
<?php endforeach; ?> 


<script > 
    $(document).ready(function() { 
     // Forum categories 
     $("#main").on("click", "#message", function() { 
      $("#getmessage").load("forum/getmessage.php", {'id': $(this).attr('data-id')}); 
     }); 
    }); 
</script > 
+0

'id属性必须是唯一的,但仍然使用'#message'。 –

+0

没有看到它,谢谢 –

+0

你会有'id = form_id'的多个表单 – Adrian