jQuery - Select option and return values

1

I have the following form:

<div class="form-group">
    <label for="sms_mensagem" class="col-sm-2 control-label">Mensagem Pré Definida</label>
    <div class="col-sm-10">
        <select class="selectpicker" id="men_cod" name="men_cod">
            <option value="">Selecione uma Mensagem</option>
            <? foreach($mensagens as $valor){ ?>
            <option value="<? echo $valor->men_cod; ?>"><? echo $valor->men_titulo; ?></option>
            <? } ?>
        </select>                                                   
    </div>
</div>                                          

<div class="form-group">
    <label for="sms_mensagem" class="col-sm-2 control-label">Mensagem</label>
    <div class="col-sm-10">
        <textarea class="form-control" id="sms_mensagem" name="sms_mensagem" rows="5"></textarea>
        <small class="caracteres">160 caracteres restantes.</small>
    </div>
</div>  

In the first select, I have pre-defined messages, which are fetched through a jQuery in the database. The moment I select one of the messages, it brings the result in the sms_message field, until then OK, running 100%.

However, if I re-select another message, it does not load the message, or if I select with null value, and then select a pre-defined message again, there is no return. What could it be?

Follow jQuery:

$(function(){
    $(document).on("change", function() {
        if(($("#men_cod").val())==''){
            $("#sms_mensagem").attr("disabled",false); 
            $("#sms_mensagem").val(''); 
        } else {
            $("#sms_mensagem").attr("disabled","disabled"); 
            var men_cod = $("#men_cod").val();
            var url = '<? echo base_url("index.php/sms/busca_mensagem"); ?>/'+men_cod;
            $.get(url, function(dataReturn){
                $('#sms_mensagem').load(url);
            });         
        }
    });           
});  
    
asked by anonymous 13.10.2016 / 14:54

2 answers

3

You should provide a selector for the "on" function:

$(function(){
    $(document).on('change', 'select', function() {
        if(($("#men_cod").val())==''){
            $("#sms_mensagem").attr("disabled",false); 
            $("#sms_mensagem").val(''); 
        } else {
            $("#sms_mensagem").attr("disabled","disabled"); 
            var men_cod = $("#men_cod").val();
            var url = '<? echo base_url("index.php/sms/busca_mensagem"); ?>/'+men_cod;
            $.get(url, function(dataReturn){
                $('#sms_mensagem').load(url);
            });         
        }
    });           
});

In this case, it will work as expected. Also, it is better to specify some element instead of "document".

    
13.10.2016 / 15:19
0
$(function(){
    $(document).on("change", function() {
        if(($("#men_cod").val())==''){
            $("#sms_mensagem").attr("disabled",false); 
            $("#sms_mensagem").val(''); 
        } else {
            $("#sms_mensagem").attr("disabled","disabled"); 
            var men_cod = $("#men_cod").val();
            var url = '<? echo base_url("index.php/sms/busca_mensagem");     ?>/'+men_cod;
            $.get(url, function(dataReturn){
                $('#sms_mensagem').html(dataReturn);
            });         
        }
    });           
}); 
    
13.10.2016 / 15:18