Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto select of Select box and Submit

Status
Not open for further replies.

aHash

Programmer
Aug 22, 2000
78
US
I have a select box. I am auto selecting if my dynamically poulated items count is 2 (first item is space and second is a legit value)

here is my code.

Code:
                            if (params.getReportCategory()!=null && params.getReportCategory().getCategoryID().equals(cat.getCategoryID())) {
                                selected = "selected";
                            } %>
                            <option value="<%=cat.getCategoryID()%>" <%=(allCategories.values().size()==2)?"selected":""%> ><%=StringUtils.escapeHTML(cat.getCategoryName())%></option><%
But once the selection is done my onchange() event does not fire. How I can make and autoselection to fire the onchange to fire?? I tried onselect but no luck.
 
Setting a different value via javascript on a select box will not fire events based on that select box.

So, after your autoselection, call the same function that you call in the onchange event.



[monkey][snake] <.
 
thanks monksnake. How can I call the same function without having a text and an anchor tag? Is it possible?
 
Code:
<script type="text/javascript"> "javascript:update()"; </script>

[code]

tried the above. Bit it says, invalid location for tage <script>  :-(
 
to call the update function, simply do:

Code:
<script type="text/javascript">update();</script>
[code]

you may need to place this just before your closing </body> tag.

 
 
*cLFlaVA
----------------------------
[tt][i]"quote goes here"[/i][/tt]
[url=http://www.coryarthus.com/][URL unfurl="true"]http://www.coryarthus.com/[/URL][/url]
 
If you have something like

<select onchange="thisFunction()">

You can call the same function within <script> tags just by doing this:

Code:
<script type="text/javascript">
thisFunction();
</script>

But looking at your code in your orginal post, are you sure you're talking about Javascript?



[monkey][snake] <.
 
Thanks to you two ClFlava, Monk.

Yes corrected the sytax and it goes into infinite looping.
Any ideas why it would do that? I am going to try placing at
just before </body> tag ..

Also monk , for your question, yes I am sure it is javascript. Below is the code for update() function, just to confirm it to you.

Code:
function update() {
document.formName.<%=ActionController.ACTION%>.value='report.ufms.UpdateUFMSFileReportSettings';
document.formName.submit();
}
 
It looks like you are infinitely submitting the form.

This is because you are calling the update function under any condition if it is encased in <script> tags like the above posts.

You need to make the form submit only in certain conditions, not in all conditions.

[monkey][snake] <.
 
This is harder than I thought. I understand that I am submitting the for infinite times.

But calling the function anywhere outside of the while loop which has <option> tag does not respond the call at all.

If I place the call to update() function right next to the
<option> tag then it goes into infinite loop. I dont see an alternative.. Here is the code part: Please tell me what I am doing wrong.

Code:
<tr>
<td class="label">Category:</td>
<td width=10></td>
<td class="table_cell">                
 <select name="reportCategory" id="reportCategory_ID" onchange="update()">
	<%  if (params.getReportCategory()==null) { %>
             <option value="">&nbsp;</option><%
          } 
			it = new SortIterator(allCategories.values().iterator(), new GenericComparator("getCategoryName", UFMSFileReportCategory.class));
			while (it.hasNext()) {
				UFMSFileReportCategory cat = (UFMSFileReportCategory) it.next();
				if (currentUser.getPermissionHandler().hasUFMSCategoryPermissions(cat.getCategoryGroup())){                    
					if (cat.isFlatFile()==isFlatFile) { 
						String selected = "";
						if (params.getReportCategory()!=null && params.getReportCategory().getCategoryID().equals(cat.getCategoryID())) {
							selected = "selected";
						} %>
						<option value="<%=cat.getCategoryID()%>" <%=(allCategories.values().size()==2)?"selected":""%> ><%=StringUtils.escapeHTML(cat.getCategoryName())%></option>
						<% [COLOR=red]//if I write the code here then infinite looping occurs [/color]
					}
				}
			}
    %>
 </select> <%
		if (params.getReportCategory()!=null) { %>
			<script type="text/javascript">update();</script><%
		}  [COLOR=red ]//this does not respond to the call [/color]
		if (params.getReportCategory()==null) { %>
			<img src="/images/attention.gif" title="Please select a report category to continue"><%
		}
	%>    
	</td>
</tr>
 
Well I looked at your code and did some testing.
Your call to update() should absolutely work. What I think is going on is params.getReportCategory is == null.

Put a Response.Write() here:
Code:
        [!]Response.Write(params.getReportCategory())[/!]
        if (params.getReportCategory()!=null) { %>
            <script type="text/javascript">update();</script><%
        }  //this does not respond to the call 
        if (params.getReportCategory()==null) { %>
            <img src="/images/attention.gif" title="Please select a report category to continue"><%
        }

See what that returns for you.

Cause everything looks fine with the code you just posted.

P.S. the place where the <script> tags currently are is the place it should be as opposed to the place where you got the infinite loop.

[monkey][snake] <.
 
grrr!!!.... interesting.

I am unable to use Response.write , As I Dont have anywhere in the related classes..

But I am sure params.getReportCategory() is not null or otherwise the image in the next if should be displayed, which is not happening.

I am trying a small trick, like this.. But
Code:
            %>
               </select> <%
                int iS=1;
               	do  { %>
               		<script type="text/javascript">update();</script>
               		
               	<%
               	iS=iS +1;
               	} while (iS==1);
                if (params.getReportCategory()==null) { %>
                    <img src="/images/attention.gif" title="Please select a report category to continue"><%
                }
            %>

The idea is to execute just once and then immediately after the while should break.. but noluck so far.. it is looping still.. will update here
 
Monk,

Actually I was wrong you were right. The gparams.getReportCategory()==null is true and the image displayed(it is very small image of 5 by 5 size.)

I added the call to update() function inside the if.

Everything works as expected. Thanks for staying with me.

Code:
               </select> <%
                if (params.getReportCategory()==null) { %>
                    <img src="/images/attention.gif" title="Please select a report category to continue">
                    <script type="text/javascript">update();</script><%
                }
            %>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top