【问题标题】:Get radio value inside iframe在 iframe 中获取单选值
【发布时间】:2010-09-16 21:58:50
【问题描述】:

我该如何使用 jQuery 来做这件事?

基本结构:

<form id="myForm">
  <iframe>
    <!-- Normal HTML headers omitted -->
    <input type=radio name="myRadio" value=1>First
    <input type=radio name="myRadio" value=2>Second
    <input type=radio name="myRadio" value=3>Third
  </iframe>
  <input type=button value="Submit" />
</form>

我尝试了网上的各种例子,例如

$("input[@type=radio][@checked]");

但是失败了。即使使用 jQuery 表单插件的 .fieldValue() 失败。

【问题讨论】:

    标签: jquery jquery-plugins


    【解决方案1】:

    试试$('#myForm iframe').contents().find('input[name=myradio]').val()

    我假设 iframe 内容已经加载并且可以访问,例如同一个域。

    【讨论】:

    • 实际上,这行得通。我使用 而不是 value=1。我的错,错字。
    【解决方案2】:

    除非 jQuery 有一些我不知道的魔法,否则访问另一个框架的 DOM 需要一些技巧。这可能有效:

    var frameDocument = $('#myForm iframe').contentDocument || $('#myForm iframe').contentWindow.document;
    $(frameDocument).find('input[type=radio][checked]');
    

    并且,请注意 jQuery 文档中的这一点:

    注意属性名称之前的“@”在 1.2 版中已被弃用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 2015-02-15
      • 1970-01-01
      • 2015-09-16
      • 2012-07-18
      相关资源
      最近更新 更多