javascript Var错误无法正常工作!点击甚至 [英] javascript Var error not working! click even

查看:85
本文介绍了javascript Var错误无法正常工作!点击甚至的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the link value or href from a popup page and add the content to the parent page in a text box. I don't knwo javascript well at all and dont understand why both links are not working.... There can be 2 items listed or there can be 500 so i need this to work with a dynamic list any help would be great! Thank you!!!!

这就是我所拥有的。

弹出页面....

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


推荐答案

同样的ids不是个好主意,所以你可以做这样,就没有必要使用id。

这是完整的代码

Same ids are not the good idea so you can do it this way, there is no need to use id.
Here is full code

这篇关于javascript Var错误无法正常工作!点击甚至的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆