這裏是一個獨立的例子。它運行後,請注意以下幾點:
- 加載的jQuery/jQueryUI的在
<head>
標籤
- 識別庫的點擊來自哪個圖像:(1)觸發點擊事件點擊
img
類的元素的任何時間,和(2)通過jQuery
- 獲得該特定IMG的ID使用AJAX到圖像ID發送給PHP處理器文件
- PHP處理器文件做一些事情,併發送回結果從PHP
- 結果接收在AJAX成功函數中。使用接收到的數據(來自PHP)的所有處理MUST必須在成功函數內發生,包括調用JQUI dlg
- JQUI dlg從TEST中的空DIV開始。PHP,其中填充有標記格蘭AJAX成功函數內部:
$('#dlgID').html(data_received_from_PHP);
- 記得關閉JQUI DLG按鈕被點擊OK時,(否則你可能只是把該命令在JQUI DLG的
close:
部分)
要運行這個簡單的例子,你需要兩個文件:
文件1:test.php的
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
$('.img').click(function() {
var iid = $(this).attr('id');
//alert('You clicked ID: ' + iid);
$.ajax({
type: "POST",
url: "yogibear.php", // "source.php",
data: "iid="+iid,
success: function(data) {
//alert('AJAX recd: ' +data);
$('#moddlg').html(data);
$('#moddlg').dialog('open');
} //END success fn
}); //END $.ajax
});
$('#moddlg').dialog({
autoOpen: false,
modal: true,
width: 400, //default is 300
title: 'This is yer dialog:',
buttons: {
'Click Me': function() {
$(this).dialog('close');
}
},
close: function() {
alert('Dlg is closing... I can do more stuff in here, including running another AJAX call');
}
});
}); //END $(document).ready()
</script>
</head>
<body>
<img id="i-1" class="img" src="http://placehold.it/140x100" />
<img id="i-2" class="img" src="http://placehold.it/140x100" />
<img id="i-3" class="img" src="http://placehold.it/140x100" />
<img id="i-4" class="img" src="http://placehold.it/140x100" />
<div id="moddlg"></div>
</body>
</html>
文件2:YOGIBEAR.PHP
<?php
$x = $_POST['iid'];
die('PHP file recd: ' . $x);
//Of course, this is where you receive the img id sent via AJAX
//and look up stuff in MySQL or etc, then return the results
//simply by putting it all into a variable and ECHOing that
//variable. Or, you can use JSON to echo an array - but make
//sure to look up a couple of examples as you must add another
//couple of params to the AJAX code block
如果你有一個codepen或小提琴,這將有助於獲得一些幫助。我確實有一個想法,但不想構建一些內容結構,這些內容結構可能與您的內容截然不同。 (相鄰的選擇器,旁邊......)thx –