【问题标题】:Value from a popup window in a parent element父元素中弹出窗口的值
【发布时间】:2013-08-18 11:12:20
【问题描述】:

我有一个包含如下元素的文档:

<input type="text" id="newsPicture" name="newsPicture" />

使用 jQuery,我创建了一个显示弹出窗口的链接。

在此弹出窗口中,我有一个新闻图片列表,例如

<a id="newsPictureName" class="newsPicture">newsPictureName.jpg</a>

现在,我将选择其中一张带有 a 元素的图片来关闭窗口

$(function(){
    $('.newsPicture').click(function() {
        window.close();
    });
});

如何将此新闻图片的名称放入父文档的输入字段中?

有什么想法吗?

对不起,我的英语不好……

【问题讨论】:

    标签: jquery select input popup window


    【解决方案1】:

    您可以通过text()访问元素的文本。

    您可以通过val()更改字段的值

    这会让你的代码变成这样

    $(function(){
        $('.newsPicture').click(function() {
            window.close();
    
            //get the text of the element we clicked
            var pictureName = $(this).text();
    
            //put this value in our field
            $('#newsPicture').val(pictureName);
        });
    });
    

    或更短的解决方案是

    $(function(){
        $('.newsPicture').click(function() {
            window.close();
    
            $('#newsPicture').val($(this).text());
        });
    });
    

    【讨论】:

    • 谢谢!但是当我使用它时,我有许多具有相同 id 名称和“newsPicture”的 a 元素,我变成了 W3C 错误。当我将 id (#) 更改为 class (.) 时,它将不起作用。
    • 首先,不允许有多个具有相同 id 的元素。另外……您已经在为您的图像使用班级新闻图片。只需选择不同的课程,例如 .picturevalue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多