Jquery dynamically update “other” option in select

后端 未结 2 723
情深已故
情深已故 2021-02-10 14:07

I\'m working on a project that will be using a lot of select menus to enter various data. I\'d like to include an \'other\' option directly in the select that will trigger a sim

2条回答
  •  没有蜡笔的小新
    2021-02-10 15:02

    This will get you started. This will add the functionality to any select on the page, appending a new value to the select (and leaving other still available in case a mistake has been made).

    $(function() {
        $('select').change( function() {
            var value = $(this).val();
            if (!value || value == '') {
               var other = prompt( "Please indicate other value:" );
               if (!other) return false;
               $(this).append('');
            }
        });
    });
    

提交回复
热议问题