使用iFrame从输入字段中获取值

时间:2014-08-14 10:05:30

标签: javascript jquery html

我有以下iframe,它是空的,直到点击提交按钮,然后找到一个人的地址并瞳孔输入字段,然后我有一些javascript从iframe中取出值来填充我的一些输入字段页面 -

我得到的问题是str1未定义的,所以它无法找到iFrame中的值...

表格

<form id="findAddressPanel">
    <input type="text" id="houseNumber">
    <input type="text" id="postCode">
    <button>Submit</button>
</form>

的iFrame

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Postcode Lookup</title>
</head>
<body>
    <input type="hidden" wicket:id="postcode" id="postcode" value=""/>
    <input type="hidden" wicket:id="line0" id="line0" value=""/>
    <input type="hidden" wicket:id="line1" id="line1" value=""/>
    <input type="hidden" wicket:id="line2" id="line2" value=""/>
    <input type="hidden" wicket:id="line3" id="line3" value=""/>
</body>
</html>

脚本在页面底部运行

$("#findAddressPanel .btn").click(function() {
    console.log('button clicked');

    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, "");
    }

    var str1 = $("#iframe").contents().find("#line0").val();

    console.log(str1);

    if (str1.trim() == "") {
      document.getElementById("unrecognisedPostcodeLookup2").className = "feedback-panel-error address-error-show";
      document.getElementById("findAddressPanel").className = "address-container-false";
    } else {
      document.getElementById("unrecognisedPostcodeLookup2").className = "feedback-panel-error address-error-hide";
      document.getElementById("addressPostCode").value = $("#iframe").contents().find("#postcode").val();
      document.getElementById("addressLine1").value = $("#iframe").contents().find("#line0").val();
      document.getElementById("addressLine2").value = $("#iframe").contents().find("#line1").val();
      document.getElementById("addressLine3").value = $("#iframe").contents().find("#line2").val();
      document.getElementById("addressLine4").value = $("#iframe").contents().find("#line3").val();
        address_finder_setter();
});

0 个答案:

没有答案