0

I have this working to populate a PRODUCT select list after selecting a CATEGORY:

jQuery ->
    $('#request_product_id').parent().hide()
    products = $('#request_product_id').html()
    console.log(products)
    $('#request_category_id').change ->
        category = $('#request_category_id :selected').text()
        options = $(products).filter("optgroup[label='#{category}']").html()
        console.log(options)
        if options
            $('#request_product_id').html(options)
            $('#request_product_id').parent().show()
        else
            $('#request_product_id').empty()
            $('#request_product_id').parent().hide()

How do I make it so the PRODUCT select list includes_blank so it doesn't default to the first value in the table?

Thanks!

4

1 回答 1

2

您可以使用 jQuery prepend( http://api.jquery.com/prepend/ ) 函数在前面添加一个空选项,如下所示:

// Create the empty option
emptyOption = $('<option />').attr('value', '');

// Prepend emptyOption to options list
options = $(products).filter("optgroup[label='#{category}']").prepend(emptyOption).html()
于 2013-07-25T23:55:04.040 回答