我对jquery比较新,不能为我的生活得到这个以返回正确的youtube id它只是总是返回表中的第一个。
PHP。 我从数据库中加载了youtube id,然后将其显示在表格中。
require_once 'library/Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
echo '<br/>';
echo '<table width="95%" cellpadding="0" cellspacing="0" align="center" border="0">';
while ($row = mysqli_fetch_assoc($videoquery))
{
$vidid = ($row['id']);
$vidtitle = ($row['title']);
$vidip = ($row['ip']);
$page2 = ($row['page']);
$q = ($row['youtubeid']);
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$videoEntry = $yt->getVideoEntry($q);
$thumbnail = '<img src="' . $videoEntry->mediaGroup->thumbnail[0]->url . '" width="100" height="92"';
echo '<tr bgcolor="#333">
<td width="100px" rowspan="2" nowrap="nowrap" style="border:2px solid #999999">'.$thumbnail.'</td>
<td colspan="2" align="left">' .$vidtitle. '
</td></tr>
<tr bgcolor="#3B3B3B"><td nowrap="nowrap">
<form method="post">
<input name="parse" type="hidden" value="playvid" /><input name="youtubeid" id="youtubeid" type="hidden" value="'.$q.'" />;
<input type="button" name="play" id="play" value="Play Video" />
</form></td>
<td align="right">
<form method="post" action="">
<input type="hidden" name="page2" value="'.$_SERVER['REQUEST_URI'].'">
<input name="parse" type="hidden" value="deletepost2" />
<input name="postid2" type="hidden" value="'.$vidid.'"/>';
if ($topuserid == $_GET['id']){
echo '<input type="submit" name="deletepost2" id="deletepost2" value="Delete Video" /';>
}
echo '</form></td></tr>';
}
echo '</table>';
}
Jquery代码
<script type="text/javascript">
$(document).ready(function(){
$('#play').live('click', function() {
$('#video').empty()
var vidid = $('#youtubeid').val();
alert(vidid);
var playvid = '<object width="300" height="193">'+
'<param name="movie" value="http://www.youtube.com/v/'+vidid+'&hl=en_US&fs=1"></param>'+
'<param name="allowFullScreen" value="true"></param>'+
'<param name="allowscriptaccess" value="always"></param>'+
'<embed src="http://www.youtube.com/v/'+vidid+'&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="193"></embed>'+
'</object>';
$('#video').append(playvid); });
});
</script>
答案 0 :(得分:1)
<script type="text/javascript">
$(document).ready(function(){
$('#play').live('click', function() {
var tr = $(this).closest('tr');
tr.find('#video').empty();
var vidid = tr.find('#youtubeid').val();
alert(vidid);
});
});
</script>
答案 1 :(得分:0)
你需要给你的选择器一些上下文。在您的html中不执行任何更改,您可以将您的js更改为
$(document).ready(function(){
var $tr = $(this).closest('tr')
$('#play').live('click', function() {
tr.find('#video').empty()
var vidid = tr.find('#youtubeid').val();
alert(vidid);
var playvid = '<object width="300" height="193">'+
'<param name="movie" value="http://www.youtube.com/v/'+vidid+'&hl=en_US&fs=1"></param>'+
'<param name="allowFullScreen" value="true"></param>'+
'<param name="allowscriptaccess" value="always"></param>'+
'<embed src="http://www.youtube.com/v/'+vidid+'&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="193"></embed>'+
'</object>';
tr.find('#video').append(playvid); });
});