javascript Var错误无法正常工作!点击事件

时间:2014-02-26 06:20:07

标签: javascript php hyperlink

我正在尝试从弹出页面获取链接值或href,并在文本框中将内容添加到父页面。我根本不知道javascript,并且不明白为什么两个链接都不起作用....可以列出2个项目或者可以有500个所以我需要这个与动态列表一起使用任何帮助都会很棒!谢谢!!!!

这就是我所拥有的。

弹出页面....

<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>

以下是主页上的代码。

<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()" />

这就是我现在拥有的......

<?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>

1 个答案:

答案 0 :(得分:0)

相同的ID不是好主意所以你可以这样做,没有必要使用id Here is full code