Trying to get a jQuery event to Fire onClick on a select/option form
I have a form, one of the fields is a select field, with 5 options. When the 5th option is clicked, I want a hidden div to be shown. I want nothing to happen with the other 4 are clicked.
It works on FireFox, but no where else. After researching it a little, it seems that Chrome/IE don't let you do the onClick firing with select. So far all suggestions have been to use onChange on the Select as a whole, but I haven't been able to figure it out to just fire the code when the 5th option is clicked using onChange. Any help would be appreciated.
Custom proptions is set to display: none; by default. I want the div id'd *custom_proptions* to show when the option id'd *custom_proptions_option* is clicked, and also want it to re-hide itself if it is shown first, and then one of the other options are clicked.
<div class="proptions_container">
<select name="proptions" id="proptions" class="proptions">
<option value="0" class="default_proptions" id="choose_prompt">Choose your Proptions…</option>
<option value="1" class="default_proptions">Yes <strong>or</strong> No</option>
<option value="2" class="default_proptions">Before <strong>or</strong> On/After</option>
<option value="3" class="default_proptions">More than <strong>or</strong> Less than</option>
<option value="4" class="default_proptions">Better <strong>or</strong> Worse</option>
<option value="5" id="custom_proptions_option">A <strong>or</strong> B (customize below)</option>
</select>
<div id="custom_proptions">
<input name="proption_1" type="text" class="text" id="proption_1" placeholder="First Proption" />
<span id="custom_or">or</span>
<input name="proption_2" type="text" class="text" id="proption_2" placeholder="Second Proption" />
</div>
<script>
$('option#custom_proptions_option').click(function() {
$('div#custom_proptions').slideDown('slow');
});
$('option.default_proptions').click(func开发者_如何学编程tion() {
$('div#custom_proptions').slideUp('slow');
});
</script>
</div>
Do it in the change event, but only for the desired value:
<script>
$('#proptions').change(function() {
if ($(this).find(':selected').val() === '5') {
$('div#custom_proptions').slideDown('slow');
} else {
$('div#custom_proptions').slideUp('slow');
}
});
</script>
The clicked
is not the right event here. You should use change
.When the event is fired, you check it's id/class(or better yet, the value in your case) and show the div if necessary.
Try using
$("selector").live('click',function(){
});
I have much better luck with it.
精彩评论