javascript变量错误无法工作!平衡点
我试图从弹出页面获取链接值或href,并将内容添加到文本框中的父页面。我根本不懂javascript,也不明白为什么这两个链接都不起作用。。。。有可能是2个项目列出或有可能是500,所以我需要这个工作与一个动态列表任何帮助将是伟大的!谢谢 这是我的 弹出页面……javascript变量错误无法工作!平衡点,javascript,php,hyperlink,Javascript,Php,Hyperlink,我试图从弹出页面获取链接值或href,并将内容添加到文本框中的父页面。我根本不懂javascript,也不明白为什么这两个链接都不起作用。。。。有可能是2个项目列出或有可能是500,所以我需要这个工作与一个动态列表任何帮助将是伟大的!谢谢 这是我的 弹出页面…… <a href="#item1" id="ddlNames" name="ddlNames"><img src="link to item 1 image"> </a> <a href="#i
<a href="#item1" id="ddlNames" name="ddlNames"><img src="link to item 1 image"> </a>
<a href="#item2" id="ddlNames" name="ddlNames"> <img src="link to item 2 image"> </a>
<input type="button" value="Select" onclick="SetName();" />
<script type="text/javascript">
function SetName() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
txtName.value = document.getElementById("ddlNames").href;
}
window.close();
}
</script>
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "<a href=\"#$value\" id=\"$value\" name=\"$value\"> $value </a>";
}
?>
<input type="button" value="Select" onclick="SetName();" />
<script type="text/javascript">
cars=<?php echo json_encode($colors); ?>;
function SetName() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
for (var i=0;i<cars.length;i++)
{
txtName.value = document.getElementById((cars[i])).href;
}
}
window.close();
}
</script>
函数SetName(){
if(window.opener!=null&&!window.opener.closed){
var txtName=window.opener.document.getElementById(“txtName”);
txtName.value=document.getElementById(“ddlNames”).href;
}
window.close();
}
这是主页上的代码。
<script type="text/javascript">
var popup;
function SelectName() {
popup = window.open("img_sel.php", "Popup", "width=300,height=100");
popup.focus();
}
</script>
<input type="text" name="image" id="txtName" />
<input type="button" value="Select Name" onclick="SelectName()" />
var弹出窗口;
函数SelectName(){
popup=window.open(“img_sel.php”,“popup”,“宽度=300,高度=100”);
popup.focus();
}
这是我现在拥有的……
<a href="#item1" id="ddlNames" name="ddlNames"><img src="link to item 1 image"> </a>
<a href="#item2" id="ddlNames" name="ddlNames"> <img src="link to item 2 image"> </a>
<input type="button" value="Select" onclick="SetName();" />
<script type="text/javascript">
function SetName() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
txtName.value = document.getElementById("ddlNames").href;
}
window.close();
}
</script>
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "<a href=\"#$value\" id=\"$value\" name=\"$value\"> $value </a>";
}
?>
<input type="button" value="Select" onclick="SetName();" />
<script type="text/javascript">
cars=<?php echo json_encode($colors); ?>;
function SetName() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
for (var i=0;i<cars.length;i++)
{
txtName.value = document.getElementById((cars[i])).href;
}
}
window.close();
}
</script>
汽车=;
函数SetName(){
if(window.opener!=null&&!window.opener.closed){
var txtName=window.opener.document.getElementById(“txtName”);
对于(var i=0;i相同的id不是一个好主意,因此可以这样做,不需要使用id。
弹出窗口中的ddlNames
标识符出现不止一次;除此之外,理论上它应该可以工作……您在控制台上是否遇到任何错误?不,它只是完全不工作。如果我取出一个Href链接,它可以工作,但不会有多个。就像我前面所说的,您不能为多个元素提供相同的id。那么……如何如果使用getElementById(“ddlNames”),我能得到其中一个的url吗?@Jack我在上面发布了我的新代码,我试图使用循环?