iframe 中的单选按钮检查

发布于 2024-09-12 02:49:46 字数 1087 浏览 1 评论 0原文

我有 javascript,它位于 iframe 中。当我检查 iframe 中的单选按钮时,父窗口会更改值。它在 firefox 上运行得很好,但在 IE 上却不行...有人能帮我解决这个问题吗?

<script type="text/javascript">
function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function asd(){
var qwer =  getCheckedValue(document.forms['uas'].elements['icon']);
window.parent.document.forms['register'].lang.value = qwer;
window.parent.document.getElementById("images").getElementsByTagName("img")[0].src = "images/lang/" + qwer  + ".png";

}
</script>

<form name="uas" method="GET" action="" onchange="asd();">
<label id="1"><input type="radio" name="icon" value="1">One</label><br><label id="2"><input type="radio" name="icon" value="2">Two ...

I have javascript, which is in iframe. When i check radio button in iframe, on parent window change value. It working perfect on firefox, but not on IE... Can someone help me with this problem?

<script type="text/javascript">
function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function asd(){
var qwer =  getCheckedValue(document.forms['uas'].elements['icon']);
window.parent.document.forms['register'].lang.value = qwer;
window.parent.document.getElementById("images").getElementsByTagName("img")[0].src = "images/lang/" + qwer  + ".png";

}
</script>

<form name="uas" method="GET" action="" onchange="asd();">
<label id="1"><input type="radio" name="icon" value="1">One</label><br><label id="2"><input type="radio" name="icon" value="2">Two ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-09-19 02:49:46

您的代码的问题是附加到表单元素的 onchange 事件,IE 不支持此。相反,您必须为每个单选按钮使用 onchange 事件:

<label id="1"><input type="radio" name="icon" value="1" onchange="asd();">One</label>
<label id="2"><input type="radio" name="icon" value="2" onchange="asd();">Two</label>

或者,您可以使用 Javascript 自动添加事件侦听器。

另外这一行:

window.parent.document.getElementById("images").getElementsByTagName("img")[0].src = "images/lang/" + qwer  + ".png";

是不正确的, getElementById 已经返回您需要的图像,因此 getElementById("images").src = ... 就可以了。

The problem with your code is the onchange event attached to the form element, IE does not support this. Instead, you have to use onchange event for each of the radio buttons:

<label id="1"><input type="radio" name="icon" value="1" onchange="asd();">One</label>
<label id="2"><input type="radio" name="icon" value="2" onchange="asd();">Two</label>

Alternatively, you can use Javascript to add the event listener automatically.

Also this line:

window.parent.document.getElementById("images").getElementsByTagName("img")[0].src = "images/lang/" + qwer  + ".png";

is not right, getElementById already returns the image you need so getElementById("images").src = ... will do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文