繁体   English   中英

如何从iframe内部的选择框中获取所选值,并将其显示在外部?

[英]How do I get the selected value from a select box, from inside an Iframe, and display it on the outside?

我在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 enjoying ' + selectVal + ' on'; 
    $("twitter").attr("id", url);
});
</script>

有什么建议吗?

澄清

  • 我的<select>元素在iframe
  • 我想在iframe之外显示所选值
  • iframe的来源与其加载页面位于同一个域

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

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

应该可能是:

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

更新:尝试使用此代码...您还需要检测iframe是否也已加载( 演示

$(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
    });
});

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM