簡體   English   中英

將jQuery-生成的變量傳遞給同一頁面中的php

[英]Pass jquery - generated variable to php in same page

我有一個腳本,當單擊該按鈕時,它會在該按鈕的行上獲取數據。 該按鈕的ID為id ='show-button'。 這是腳本:

<script>
        $(document).ready(function(){
             $(".show-button").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname 
             var lecturer_id = names."_".surname;
             $("#show_dialog").dialog({autoOpen: false});
             $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});
         });
        });
    </script>

最后兩行顯示一個jquery對話框。 我的意思是這些行:

$("#show_dialog").dialog({autoOpen: false});
$(".show-button").on("click", function() {$("#show_dialog").dialog("open");});

現在,我需要將varcturer_id的值傳遞給該代碼之外但位於同一文檔內的php腳本。 此php代碼將生成由這兩行創建的對話框的內容。 假設我只想回顯在對話框中傳遞的變量(使用php)。

關於如何使其工作的任何想法?

您的問題不是100%清楚,但是,如果我答對了,那只是個主意:

<script>
$(document).ready(function(){

     $(".show-button").click(function() {
         var $row = $(this).closest("tr");    // Find the row
         var names = $row.find(".name").text(); // Find the name
         var surname = $row.find(".surname").text(); // Find the surname 
         var lecturer_id = names."_".surname;
        $.post( "test.php", { names: names, surname: surname; lecturer_id: lecturer_id })
            .done(function( data ) {
            $("#show_dialog")[0].innerHTML = data ;
            $("#show_dialog").dialog({autoOpen: false});
            $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});
        });

    });
});
    </script>

我同意@JayBlanchard,您甚至不需要在這里進行任何Ajax調用,只需生成html即可:

 $(document).ready(function(){

         $(".show-button").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname 
             var lecturer_id = names."_".surname;

            $("#show_dialog")[0].innerHTML = ' Name = '+names +'; Surname = '+surname ;
                $("#show_dialog").dialog({autoOpen: false});
                $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});


        });
    });

您可以使用jQuery post或ajax。

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

test.php將是php的接收端,它期望由jquery發送的數據。
{ name: "John", time: "2pm" }將是您希望發送到php的數據。
data將是php輸出的數據。

請參閱http://api.jquery.com/jquery.post/了解更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM