Print Page | Close Window

Change selection in a dropdown

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=2110
Printed Date: 23 Apr 2019 at 1:41am
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Change selection in a dropdown
Posted By: jhmcmullen
Subject: Change selection in a dropdown
Date Posted: 31 Dec 2016 at 3:51pm
Okay, I'm not wrapping my head around this.

I have a listbox which contains a space and the numbers one through ten. That works fine; nothing needs to be triggered off the value if they choose a number or zero it out. Currently, the listbox is set to commit the selected value immediately, but

I would like to add a selection item to the listbox, probably a question mark, which randomly chooses a number between one and ten and sets the value of the listbox accordingly.

To me, this sounds like a keystroke or validation function, something like,

if (event.value == "?") {
     event.value = getLevel();
    }

Except doing that doesn't seem to work. (I've tried it as both.) I get just the ? character in the field.

Then I tried going around, and using this.getField("name").currentSelectedIndices to set it, but that didn't work. (getLevel() is written and works fine; I use it elsewhere in the same document.)

So I'm doing something wrong.

It doesn't help that I'm (a) using a different cheaper program to edit the PDF, rather than Acrobat, and (b) I find that my mind is Teflon to the whole set of details regarding events and listboxes, so I end up having to re-learn whatever bit of it I need.

So what should I be doing instead?

Thanks for any help.

John



Replies:
Posted By: gkaiseril
Date Posted: 01 Jan 2017 at 4:44pm
Have you looked at the Acrobat JavaScript API and the changeEx property?



Posted By: Rob Lyman
Date Posted: 04 Jan 2017 at 9:41pm
I think what you're seeing is that the 'Selection Change' event (which is what the 'Keystroke' event is called for a list box) is called in two different ways at different points while the user is interacting with the form:

1.) When the user selects an item in the list box, an interim event is fired. This contains the current value (i.e. the value prior to selection) in 'event.value', and the newly selected value in 'event.change'. You can tell it's an interim event because 'event.willCommit' is false.

2.) When the user commits the item (by clicking another form control, or in fact anywhere outside of the list box), a commit event is fired. This contains the newly selected value in 'event.value' and a null in 'event.change'. In this case, 'event.willCommit' is true.

Note that several interim keystroke events may fire before the commit event; this will happen if the user changes their selection before clicking outside of the list box.

To get the effect you want, try first checking the 'event.willCommit' property. If it's true, then change 'event.value', which will replace the list box's value (you may also need to update the current selection index for the list box).


-------------
Rob Lyman
Software Engineer
http://www.datalogics.com



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net