简体   繁体   中英

How to clear form behind modal window with javascript?

Just like the question says, I'm trying to clear a form from a modal window while the modal stays up. I've tried:

if (myDocument.title == "Modal Window") {
    parent.document.getElementById("textbox")
}

(I need it to do more than 1 tb, but used that just to try to get there. No luck.

It is contained within an iFrame, so I tried:

if (myDocument.title == "Modal Window") {
    var ifr = document.getElementById("iFrame")
    var form = ifr.document.getElementById("form")
    ClearForm(form)
}

The ClearForm(form) function I stole from another Stack Overflow answer:

function ClearForm(form) {
    $(':input', form).each(function () {
        var type = this.type;
        var id = this.id;
        if (type == 'text' && id != 'text2')
            this.value = "";
    });
}

That 'text2' is one specific tb that we need to remain populated.

Any idea what I'm missing? I've been plagued with this bug for weeks.

I expect your issue is that the form is within an iFrame - most browsers won't allow you to modify elements within an iFrame, from the parent page, if they aren't from the same origin (or if the server is set up to deny it, or if you're looking at the page locally... see here for more details)

To double-check, try moving the form markup into the same page as the modal is in and run your function ClearForm from there. I expect that you'll then find it works.

Your only way around this would be to include the ClearForm function within the iFrame'd page, and then trigger it from the parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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