[英]Save Img Src to Javascript Variable
我试图将图像src设置在从表单传递的隐藏变量中。 当我输出变量是“未定义”。 我对Java语言很陌生,不知道可能是什么问题。 我的错误在网上:
document.sampleForm.image.value = "images/"+img.attr('src')+"";
使用Javascript:
$('tr', '#target').each(function () {
var tr = $(this);
html += '<center><tr>';
$('td', tr).each(function () {
var td = $(this);
if (!td.is('td.header')) {
var img = $('img', td);
var text = $('textarea', td).val();
html += '<td style="width: 33%;">';
html += (img.length) ? '<img src="' + img.attr('src') + '" style="display: block; max-width: 100%;" />' : '<div style="width: 100%;"></div>';
document.sampleForm.image.value = "images/"+img.attr('src')+"";
document.forms["sampleForm"].submit();
html += '</td>';
}
});
html += '</tr></center>';
});
HTML:
<table id="target">
<form id="sampleForm" name="sampleForm" action="test.php" method="post">
<tr><center>
<td class="logo" colspan="3">
<h3>Choose Header</h3>
<div class="img-container"><input type="hidden" name="image" id="image" value=""></div>
</td>
</center>
</tr>
<tr>
<td style="height:auto;">
<?php
$test = mysql_query("SELECT id, title FROM tmpl2");
echo "<select class='image_select' id='logo_option' name='logo_option[]' multiple='multiple'>";
while ($row = mysql_fetch_assoc($test))
{
echo "<option value='".$row[id]."'>$row[title]</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr><center>
<td class="logo" colspan="3">
<h3>Choose Logo</h3>
<div class="img-container"></div>
</td>
</center>
</tr>
<tr>
<td style="height:auto;">
<?php
$test = mysql_query("SELECT id, title FROM tmpl2");
echo "<select class='image_select' id='header_option' name='header_option[]' multiple='multiple'>";
while ($row = mysql_fetch_assoc($test))
{
echo "<option value='".$row[id]."'>$row[title]</option>";
}
echo "</select>";
?>
</td>
</tr>
</table>
您变得不确定的原因是
document.sampleForm.image.value = "images/"+img.attr('src')+"";
不是HTMLImageElement的属性。 您需要.src
。
document.sampleForm.image.src = "images/"+img.attr('src')+"";
但是正如@Barmar所说,您真的不应该这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.