Clear input and insert new value into it

Asked

Viewed 64 times

0

The combobox highlighted in red when selected triggers an action in jquery to take values from other inputs and add them to the input in blue (Hidden type, but this text for you to see what is happening) so far so good, problem that if it is clicked again instead of updating the value of the blue input, it is adding again the new values to the old values. I tried user Empty and nothing, already tried to clear the value of the blue input and add again the values and nothing

inserir a descrição da imagem aqui

      var scp_opts = {
    width: 260,
    matchContains: true,
    selectFirst: false,
    appentTo: '#input_configurations_plan',
    source: function(request, response){
       let plan  = $(".form_union_plans:last").val();
       let split = plan.split("|");

      $.ajax({
        url: getUrl + "filter/code_per_plan",
        type: 'get',
        dataType: 'html',
        data:{'term' : request.term, 'table' : split[1] }
      }).done(function( codePlan ){

          if( codePlan.length > 0 ){

             codePlan = codePlan.split( ',' );

             response( $.each( codePlan, function( key, item ){
                return({
                    label: item  
                });

             }));
          }
      });
    },
    select: function( event, ui ) { 
       var plan   = $(".form_union_plans:last").val();
       var split  = plan.split("|"); 
       var ch     = $(".insert-factor-multiplier:last").val();
       var number = $(".insert-ch:last").val();
       var  values = '';
      $.ajax({
        url: getUrl + "filter/value_code_per_plan",
        type: 'get',
        dataType: 'json',
        data:{'term' : ui.item.value, 'table': split[1]}
      }).done(function( response ){
        var code   = $(".search-code-plan:last").val();
        if( response.value != "" ){
         $(event.target).closest('tr').find('td .insert-value-code').val( response.value.replace('.', ',').replace(/(\d)(?=(\d{3})+\,)/g, "$1.") );                
         var money = response.value.replace('.', ',').replace(/(\d)(?=(\d{3})+\,)/g, "$1.");                
        }

          values = plan +'|'+ ch +'|'+ number +'|'+ code +'|'+ money;

        if( ( count++ ) == 0 ){

         $(event.target).closest('tr').find('td .form_union_plans').val( values ); 

        }else{
          $(event.target).closest('tr').find('td .form_union_plans').val("");  
          $(event.target).closest('tr').find('td .form_union_plans').val( values);  
        }

      });

    }                    

}

  • And what is values... Wouldn’t that be the problem? You shouldn’t be restarting your values...

  • @fernandosavio values is the sum of all inputs. How would you restart them?

  • Can you provide a [mcve]? Your problem has been well explained, but you show almost nothing of code and what you clearly show is not the source of the problem. Any doubt always has the: [Ask]

  • I’ve tried that way @Sam and it didn’t work

  • You have to show the whole code. Only with the snippet you can’t say anything.

  • I edited @Sam the question with snippet of the code, when selecting the select it plays in the last input two values and then when the red code of the image is chosen it adds the remaining values.

  • @Sam But then it won’t work either, already tried it. Would have to clear the value of the input FORM_UNION_PLANS and update it with the values again. I can clean and put a setTimeout function to pick up the values again and insert, but they are going duplicated again :(

  • What is this #input_configurations_plan?

  • @Sam is the body id of the table, where these inputs are, to assign each new row within the trs! I don’t know why it is leaving VALUE empty, but at the time of entering the data again it picks up the previous one, but as if it was " emptied " ?

  • 1

    I’m sure that’s the problem plan... Here var plan = $(".form_union_plans:last").val(); you take the value of the last one and then split to send to AJAX, and in the done plan remains the same.

  • @Sam I changed the VAR PLAN which Voce quoted and passed another class value $(".Rule-Choose-plan-doctor:last"). val(), replacing union_plans and it worked, so I was able to remove the final if. Thanks for the patience and help. How do I flag it as solved and mark Voce as the help author? I’m new here and don’t know very well yet fiddle with the stack

  • Cool that solved. Dude, either you leave as is or you can put an answer yourself.

Show 7 more comments

1 answer

0

Solved by changing var plan = $(".form_union_plans:last"). val(); by var plan = $(".Rule-Choose-plan-doctor:last"). val(); Before each click was taking the whole value of form_union_plans and inserting it in itself, that was the error! With the value of the Rule-Choose-plan-doctor class it changes form_union_plans only with the data they need. Thanks @Sam

Browser other questions tagged

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