将 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");});
现在,我需要将 var lecturer_id 的值传递给此代码之外但在同一文档内的 php 脚本。此 php 代码将生成由这两行创建的对话框的内容。假设我只想回显对话框中传递的变量(使用 php)。
知道如何让它发挥作用吗?
您可以使用 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.
输出的数据
你的问题不是 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");});
});
});
我有一个脚本,可以在单击按钮时获取按钮行中的数据。按钮的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");});
现在,我需要将 var lecturer_id 的值传递给此代码之外但在同一文档内的 php 脚本。此 php 代码将生成由这两行创建的对话框的内容。假设我只想回显对话框中传递的变量(使用 php)。
知道如何让它发挥作用吗?
您可以使用 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.
你的问题不是 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");});
});
});