How to get a drop down selected text from an ifram

2019-06-08 17:31发布

问题:

I have an iframe, which opens up in a modal. The contents are present in iframe.jsp. Now the iframe contains a drop down. On change of drop down i will call another window. However, if the user selects the option : camRip in the drop down options, then that window will not be called.

My code for on change looks like this :

<select id="hamStan"  onchange = "callWindow('<%=URL %>');">

My on change is like this :

function callWindow(URL) {
var selectedByUser = document.getElementById("hamStan");
var strUserText = selectedByUser.options[selectedByUser.selectedIndex].text;
if(strUserText!="camRip ")
{
var windowName = "popUp";    
var windowSizeArray = [ "width=740,height=400,top=10,left=50,toolbar=1,location=1,directories=0,status=0,menuBar=1,scrollBars=1,resizable=1"];
window.open(URL,windowName,windowSizeArray);
}
}

But the problem is selectedByUser is coming up as null. Where am i going wrong ? Kindly help . How do i get the drop down user selected value on change from the modal iframe.jsp ?

回答1:

After a lot of research i finally found the solution .

This is what i fished up :

function callWindow(URL) {
var iframe = window.parent.parent.document.getElementById('myIframe');
var iwin= iframe.contentWindow || iframe.contentDocument.defaultView;
var selectedByUser  = iwin.document.getElementById('hamStan');
var strUserText = selectedByUser.options[selectedByUser.selectedIndex].text;
if(strUserText!="camRip ")
{
var windowName = "popUp";    
var windowSizeArray = [ "width=740,height=400,top=10,left=50,toolbar=1,location=1,directories=0,status=0,menuBar=1,scrollBars=1,resizable=1"];
window.open(URL,windowName,windowSizeArray);
}
}

Be sure here that you are correctly accessing the iframe-id otherwise variable iframe will give you a type error. Hope it helps others.