2017-06-01 26 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