jQuery - Select option and return values

Asked

Viewed 203 times

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 predefined messages, which are searched 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 again select another message, it does not load the message, or if I select with null value, and then again select a predefined message, there is no return. What could be?

Follow the 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);
            });         
        }
    });           
});  
  • What will suffer the event is the document or the element in it?

  • What goes through the event is the element, in this case, "men_cod", if it is null, the field is opened to type the message, if it is true, comes from the database the message already ready, with disable for it not to edit

2 answers

3

You must 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".

  • In this case, it also does not return me, when I return it to the , and select another message

  • Check if you are not caching, if you use $.ajaxSetup ({cache: false });

  • So, and another detail, if I do not select any and fill out a loose message, in the POST does not come, but if I remove the script, it returns...

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);
            });         
        }
    });           
}); 
  • In this case, it also does not return me, when I return it to the , and select another message

Browser other questions tagged

You are not signed in. Login or sign up in order to post.