HTML form readonly SELECT tag/input

前端 未结 30 3074
陌清茗
陌清茗 2020-11-22 08:28

According to HTML specs, the select tag in HTML doesn\'t have a readonly attribute, only a disabled attribute. So if you want to keep

30条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 09:09

    You should keep the select element disabled but also add another hidden input with the same name and value.

    If you reenable your SELECT, you should copy its value to the hidden input in an onchange event and disable (or remove) the hidden input.

    Here is a demo:

    $('#mainform').submit(function() {
        $('#formdata_container').show();
        $('#formdata').html($(this).serialize());
        return false;
    });
    
    $('#enableselect').click(function() {
        $('#mainform input[name=animal]')
            .attr("disabled", true);
        
        $('#animal-select')
            .attr('disabled', false)
        	.attr('name', 'animal');
        
        $('#enableselect').hide();
        return false;
    });
    #formdata_container {
        padding: 10px;
    }
    
    

提交回复
热议问题