开发者

Handling "other" option in dropdown select with free text input field

开发者 https://www.devze.com 2022-12-29 04:15 出处:网络
I\'m trying to figure out the best way to handle a dropdown list where one of the options \"other\" shows a hidden text field (via jQuery), where the user can then enter text. Should both the dropdown

I'm trying to figure out the best way to handle a dropdown list where one of the options "other" shows a hidden text field (via jQuery), where the user can then enter text. Should both the dropdown and the input field be given the same "name" attribute, then server side code runs a check on the values of each in order to know what one is the active value - e.g if the value of the select box is "other", then check the value of the text input field - if this isn't the default value ("enter text"), then it's ok to use this value and save it to the database. Is this a messy approach to this problem?

EDIT: this is my client side code for show/hiding the "other" field.

开发者_如何转开发
$('.select_change').live("click change", function(){
            //check if value is other
            $(this).parent().find(".hidden").toggle($(this).val() == 'other');
        });


I think the way to go is to check if the value selected is equal to "Other" and then show/hide the input based on the value.

On the server side I would perform a similar check, and store the value from the textbox if the dropdown value is equal to "Other".

I haven't tested this code but I think this is the general idea:

$('#myDrpdown').change(function() {
    if($(this).val() === "other"){
        $("#hiddenInput").show();
    } else {
        $("#hiddenInput").hide();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消