如何从Iframe中的选择框中获取选定的值,并将其显示在外部? [英] How do I get the selected value from a select box, from inside an Iframe, and display it on the outside?

查看:82
本文介绍了如何从Iframe中的选择框中获取选定的值,并将其显示在外部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML中有这个选择框,并且它所在的页面正通过iframe加载到另一个页面(都在我的网站上)。我希望能够从选择框(在iframe中)获取选定的值,并将其显示在外部。以下是我拼凑在一起的一些代码,但我不知道我是否正确地做了这件事:

 < script> ; 
$('#iframe')。contents()。find('#cds')。change(function(){
var selectVal = $(this).val();
url ='https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoy'+ selectVal +'on';
$(twitter)。attr(id,url) ;
});
< / script>

有什么建议?

澄清




  • 我的< select> 元素位于 iframe

  • 我想在 iframe之外显示选定的值

  • iframe的来源与加载它的页面位于同一个域中

  • 我不确定你的HTML是什么样的,但是

    <$ p
    $ p> $(twitter)。attr(id,url);

    应该是:

      $(。twitter)。attr(id,url); 






    更新:尝试此代码...您需要检测iframe是否已加载( demo

      $(function(){
    //定义这里是因为我们改变了ID
    var $ twitter = $('# ('load',function(){
    $(this).contents()。);
    //绑定到iframe中选择
    $('#iframe')。 find('#cds')。change(function(){
    var selectVal = $(this).val();
    url ='https://twitter.com/intent/tweet?button_hashtag = stream& text =仅仅享受'+ selectVal +'on';
    $ twitter.attr(id,url).text(url);
    })。change(); //触发更改为初始值
    });
    });

    另外,由于我们正在更改Twitter的ID,因此我们需要保存该jQuery对象。 / p>

    I have this select box in HTML, and the page it's on is being loaded into another page (both on my website) through an iframe. I want to be able to get the selected value from the select box (inside the iframe), and display it on the outside. Here's some code I've pieced together, but I don't know if i'm doing it right:

    <script>
    $('#iframe').contents().find('#cds').change(function() {
        var selectVal = $(this).val();
        url = 'https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoying ' + selectVal + ' on'; 
        $("twitter").attr("id", url);
    });
    </script>
    

    Any advice?

    To Clarify

    • My <select> element is in the iframe
    • I want to display the selected value outside the iframe
    • The source of the iframe is on the same domain as the page into which it is loaded

    解决方案

    I'm not sure what your HTML looks like, but

    $("twitter").attr("id", url);
    

    should probably be:

    $(".twitter").attr("id", url);
    


    Update: Try this code... you need to detect if the iframe has loaded as well (demo)

    $(function () {
        // define this here because we're changing the ID
        var $twitter = $('#twitter');
        // bind to select inside iframe
        $('#iframe').on('load', function () {
            $(this).contents().find('#cds').change(function () {
                var selectVal = $(this).val();
                url = 'https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoying ' + selectVal + ' on';
                $twitter.attr("id", url).text(url);
            }).change(); // trigger change to get initial value
        });
    });
    

    Also, since we're changing the ID of twitter, we need to save that jQuery object.

    这篇关于如何从Iframe中的选择框中获取选定的值,并将其显示在外部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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