Error in Javascript variable

Asked

Viewed 68 times

0

I have the following javascript code below which occurs the following error

Uncaught Referenceerror: rdIdem is not defined at Htmlinputelement (ff_admin.php? Folder=requests/&file=ff_fmins_requests&ext=php:806) at Htmldocument.Dispatch (jquery.js:4435) at Htmldocument.r.Handle (jquery.js:4121).

Where it is called an input type radio. You can assist me in solving this error? Complete code of the entire following page, (remembering that the function that is happening the error is "rdDesIdemRe"):

    <script type="text/javascript">

  $(document).on("change","input[type=radio]",function(){
    var rdRetirar = $('[name="rdRetirar"]:checked').val();

    if(rdRetirar=='sim'){
      $('#rdDestinaIdemRemeS').prop("disabled", true);

      $('#rdDestinaIdemRemeN').prop("disabled", true);

      $('#seldestinatario').prop("disabled", true);
      $('#txtemaild').prop("disabled", true);
      $('#telefone1d').prop("disabled", true);
      $('#telefone2d').prop("disabled", true);
      $('#selEstadod').prop("disabled", true);
      $('#selCidaded').prop("disabled", true);
      $('#selBairrod').prop("disabled", true);
      $('#logradourod').prop("disabled", true);
      $('#numerod').prop("disabled", true);
      $('#complementod').prop("disabled", true);
      //Desabilita os campos de relacionados a terceirizada
      $('#selTerceirizada').prop("disabled", true);
      $('#selVeiculo').prop("disabled", true);
      $('#txtvalortaxa').prop("disabled", true);
      $('#txtvalortaxa').val("0,00");
    }else{
      //Reabilita os campos do destinatario
      $('#rdDestinaIdemRemeS').prop("disabled", false);

      $('#rdDestinaIdemRemeN').prop("disabled", false);

      $('#seldestinatario').prop("disabled", false);
      $('#txtemaild').prop("disabled", false);
      $('#telefone1d').prop("disabled", false);
      $('#telefone1d').prop("required", true);
      $('#telefone2d').prop("disabled", false);
      $('#selEstadod').prop("disabled", false);
      $('#selCidaded').prop("disabled", false);
      $('#selBairrod').prop("disabled", false);
      $('#logradourod').prop("disabled", false);
      $('#numerod').prop("disabled", false);
      $('#complementod').prop("disabled", false);
      //Reabilita os campos de relacionados a terceirizada
      $('#selTerceirizada').prop("disabled", false);
      $('#selVeiculo').prop("disabled", false);
      $('#txtvalortaxa').prop("disabled", false);
      if(rdIdem=="nao"){
        $('#seldestinatario').prop("required", true);
        $('#selEstadod').prop("required", true);
        $('#selCidaded').prop("required", true);
        $('#selBairrod').prop("required", true);
      }
    }


  });


</script>

<div class="row">
  <div class="col-lg-12">
    <h1 class="page-header">Registro de Pedidos</h1>
  </div>
</div>
<div class="alert alert-danger" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Erro:</span>
    Os campos identificados com  *  são obrigatórios!
  <br>
</div>
<form name="frmpedidos" data-toggle="validator" method="POST" action="?folder=pedidos/&file=ff_ins_pedidos&ext=php" role="form" onsubmit="return validaDetalhe()">
  <div class="row">
    <div class="col-lg-12">
      <div class="col-md-6 col-lg-6">
        <div class="form-group row">
            <label class="col-lg-2 form-control-label">*Data:</label>
            <div class="col-lg-10 ">
              <input type="text" name="txtdata" class="form-control datas_pedidos" placeholder="dd-mm-aaaa" required  id="datepicker">
              <div class="help-block with-errors"></div>
            </div>
          </div>
        </div>
        <div class="col-md-6 col-lg-6">
          <div class="form-group row">
            <label class="col-lg-2 form-control-label">*Horário:</label>
            <div class="col-lg-10">
              <!-- <input type="time" name="txthorario" class="form-control datas_pedidos" placeholder="dd-mm-aaaa" required> -->
              <select class="form-control select-basic" type="text"  name="txthorario" required >
                <option value="">Selecione...</option>
                <option value="01:00 - 02:00">01:00 - 02:00</option>
                <option value="02:00 - 03:00">02:00 - 03:00</option>
                <option value="03:00 - 04:00">03:00 - 04:00</option>
                <option value="04:00 - 05:00">04:00 - 05:00</option>
                <option value="05:00 - 06:00">05:00 - 06:00</option>
                <option value="06:00 - 07:00">06:00 - 07:00</option>
                <option value="07:00 - 08:00">07:00 - 08:00</option>
                <option value="08:00 - 09:00">08:00 - 09:00</option>
                <option value="09:00 - 10:00">09:00 - 10:00</option>
                <option value="10:00 - 11:00">10:00 - 11:00</option>
                <option value="11:00 - 12:00">11:00 - 12:00</option>
                <option value="12:00 - 13:00">12:00 - 13:00</option>
                <option value="13:00 - 14:00">13:00 - 14:00</option>
                <option value="14:00 - 15:00">14:00 - 15:00</option>
                <option value="15:00 - 16:00">15:00 - 16:00</option>
                <option value="16:00 - 17:00">16:00 - 17:00</option>
                <option value="17:00 - 18:00">17:00 - 18:00</option>
                <option value="18:00 - 19:00">18:00 - 19:00</option>
                <option value="19:00 - 20:00">19:00 - 20:00</option>
                <option value="20:00 - 21:00">20:00 - 21:00</option>
                <option value="21:00 - 22:00">21:00 - 22:00</option>
                <option value="22:00 - 23:00">22:00 - 23:00</option>
                <option value="23:00 - 24:00">23:00 - 00:00</option>
                <option value="00:00 - 01:00">00:00 - 01:00</option>
              </select>
              <div class="help-block with-errors"></div>
            </div>
          </div>
        </div>
        <div class="col-lg-12" >
          <div class="form-group row col-lg-12" >
              <label>*Deseja Retirar na Loja? (Isento de Taxas de Entrega)</label>
              <label class="radio-inline">
                <input type="radio" name="rdRetirar" id="rdRetirar" value="nao"  required> NÃO
              </label>
              <label class="radio-inline">
                <input type="radio" name="rdRetirar" id="rdRetirar" value="sim"  onload="RetirarLoja()"> SIM
              </label>
              <div class="help-block with-errors"></div>
          </div>
        </div>

      <hr class="hr">
      <div class="panel panel-default">
        <div class="panel-body">
          <div class="panel-group" id="accordion">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h4 class="panel-title">
                  Remetente
                </h4>
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                  <li class="fa fa-minus-square fa-2x navbar-right minimizar"  alt="" width="30px" height="30px"></li>
                </a>
              </div>
              <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Nome:</label>
                      <div class="col-sm-8">
                        <select name="selremetente" id="selremetente" class="select-tag form-control" maxlenght="45" style="width: 100%;" onChange="buscaremetente()" required>
                          <option value="">Escolha um cliente...</option>
                          <?php
                            $sql_sel_clientes_preparado->execute();

                            while($sql_sel_clientes_dados = $sql_sel_clientes_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_clientes_dados['id'];?>"><?php  echo $sql_sel_clientes_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">E-mail:</label>
                      <div class="col-sm-8">
                        <input type="email" id="txtemail" name="txtemail" class="form-control" maxlength="70" placeholder="[email protected]">
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Telefone Fixo:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone1" id="telefone1" class="form-control" maxlength="20" placeholder="34391090" pattern="^([0-9]{1,20})$" required>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Telefone Celular:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone2" id="telefone2" class="form-control" maxlength="20" pattern="^([0-9]{1,20})$" placeholder="99445218123">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Estado:</label>
                      <div class="col-sm-8">
                        <select name="selestado" id="selEstado" class="select-basic form-control" style="width: 100%;" onChange="mostrarcidade()">
                          <option value="">Escolha um Estado..</option>
                          <?php
                            $sql_sel_estados_preparado->execute();

                            while($sql_sel_estados_dados = $sql_sel_estados_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_estados_dados['id'];?>"><?php  echo $sql_sel_estados_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Cidade:</label>
                      <div class="col-sm-8">
                        <select name="selcidade" id="selCidade" class="select-basic form-control" style="width: 100%;" onchange="mostrarbairro()">
                          <option value=''>Escolha um Estado Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Bairro:</label>
                      <div class="col-sm-8">
                        <select name="selbairro" id="selBairro" class="select-basic form-control" style="width: 100%;">
                          <option value=''>Escolha uma Cidade Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Logradouro:</label>
                      <div class="col-sm-8">
                        <input type="text" id="txtlogradouro" name="txtlogradouro" class="form-control" placeholder="Marques de Olinda" maxlength="45">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Número:</label>
                      <div class="col-sm-8">
                        <input type="text" id="txtnumero" name="txtnumero" class="form-control" maxlength="6" placeholder="1222">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">CPF:</label>
                      <div class="col-sm-8">
                        <input type="text" id="cpf" name="txtcpf" class="form-control" maxlength="11" pattern="^[0-9]{3}[0-9]{3}[0-9]{3}[0-9]{2}$" placeholder="01234567890">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <!--/. div col-md-12 col-lg-6  -->
                  <div class="form-group row col-lg-12">
                    <label class="col-sm-2 form-control-label" >Comentário / Complemento:</label>
                    <div class="col-sm-10">
                      <textarea type"text" name="txtacomplemento" id="complemento" class="form-control" placeholder="Comentário/Complemento do endereço do Remetente..."></textarea>
                    </div>
                  </div>
              </div>
            </div>

          </div>
          <!-- /.panel-collapse collapse in -->
          <div class="panel panel-default">
              <div class="panel-heading">
                <h4 class="panel-title">
                  Destinatário
                </h4>
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <li class="fa fa-minus-square fa-2x navbar-right minimizar"  alt="" width="30px" height="30px"></li>
                </a>
              </div>
              <div id="collapseTwo" class="panel-collapse collapse">
                <div class="panel-body">
                  <div class="col-lg-12">
                    <script type="text/javascript">
                      function rdDesIdemRem(){
                        var rdIdem = $('[name="rdDestinaIdemReme"]:checked').val();
                        if(rdIdem=="sim"){

                           var selremetente = $('#selremetente').val();
                           $('#seldestinatario').val(selremetente);
                           $('#seldestinatario').prop("required", false);
                           var txtemail = $('#txtemail').val();
                           $('#txtemaild').val(txtemail);
                           var telefone1 = $('#telefone1').val();
                           $('#telefone1d').val(telefone1);
                           var telefone2 = $('#telefone2').val();
                           $('#telefone2d').val(telefone2);
                           var selEstado = $('#selEstado').val();
                           $('#selEstadod').val(selEstado);
                           $('#selEstadod').prop("required", false);
                           var selCidade = $('#selCidade').val();
                           $('#selCidaded').val(selCidade);
                           $('#selCidaded').prop("required", false);
                           var selBairro = $('#selBairro').val();
                           $('#selBairrod').val(selBairro);
                           $('#selBairrod').prop("required", false);
                           var logradouro = $('#txtlogradouro').val();
                           $('#logradourod').val(logradouro);
                           var numero = $('#txtnumero').val();
                           $('#numerod').val(numero);
                           var complemento = $('#complemento').val();
                           $('#complementod').val(complemento);

                           if(selEstado==""){
                             alert("Favor preencher o campo Estado na seção do Remetente!");
                             $("#rdDestinaIdemRemeN").prop("checked", true);
                           }else if(selCidade==""){
                             alert("Favor preencher o campo Cidade na seção do Remetente!");
                             $("#rdDestinaIdemRemeN").prop("checked", true);
                             }else if(selBairro==""){
                               alert("Favor preencher o campo Bairro na seção do Remetente!");
                               $("#rdDestinaIdemRemeN").prop("checked", true);
                               }else if((logradouro=="") && (complemento=="")){
                                 alert("Favor preencher o campo Logradouro ou Complemento na seção do Remetente!");
                                 $("#rdDestinaIdemRemeN").prop("checked", true);
                                 }else if((numero=="") &&(complemento=="")){
                                   alert("Favor preencher o campo Número ou Complemento na seção do Remetente!");
                                   $("#rdDestinaIdemRemeN").prop("checked", true);
                                 }


                        }else{
                          $('#seldestinatario').val("");
                          $('#txtemaild').val("");
                          $('#telefone1d').val("");
                          $('#telefone2d').val("");
                          $('#selEstadod').val("");
                          $('#selCidaded').val("");
                          $('#selBairrod').val("");
                          $('#logradourod').val("");
                          $('#numerod').val("");
                          $('#complementod').val("");
                        }
                      }
                    </script>
                    <div class="form-group row">
                      <label class="col-sm-5 form-control-label">* O Destinatário é o mesmo que o Remetente?:</label>
                      <div class="col-sm-7">
                        <input type="radio" name="rdDestinaIdemReme" id="rdDestinaIdemRemeS" value="sim"  required onClick="rdDesIdemRem()"> SIM ou
                        <input type="radio" name="rdDestinaIdemReme" id="rdDestinaIdemRemeN"  value="nao"  onClick="rdDesIdemRem()" checked> NÃO
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12" id="DivdoDestinatario">
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Nome:</label>
                      <div class="col-sm-8">
                        <select name="seldestinatario" id="seldestinatario" class="select-tag form-control" maxlenght="45" style="width: 100%;" onChange="buscadestinatario()" >
                          <option value="">Escolha um Destinatario...</option>
                          <?php
                            $sql_sel_clientes_preparado->execute();
                            while($sql_sel_clientes_dados = $sql_sel_clientes_preparado->fetch()){
                          ?>
                            <option value="<?php echo $sql_sel_clientes_dados['id'];?>"><?php  echo $sql_sel_clientes_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">E-mail:</label>
                      <div class="col-sm-8">
                        <input type="email" id="txtemaild" name="txtemaild" class="form-control" maxlength="70"  placeholder="[email protected]">
                        <div class="help-block with-errors"></div>

                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Telefone Fixo:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone1d" id="telefone1d" class="form-control" pattern="^([0-9]{1,20})$" maxlength="20" placeholder="34391090" >
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Telefone Celular:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone2d" id="telefone2d" class="form-control" maxlength="20" pattern="^([0-9]{1,20})$" placeholder="955123231351">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Estado:</label>
                      <div class="col-sm-8">
                        <select name="selestadod" id="selEstadod" class="select-basic form-control" style="width: 100%;" onChange="mostrarcidaded()" >
                          <option value="">Escolha um Estado..</option>
                          <?php
                            $sql_sel_estados_preparado->execute();
                            while($sql_sel_estados_dados = $sql_sel_estados_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_estados_dados['id'];?>"><?php  echo $sql_sel_estados_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Cidade:</label>
                      <div class="col-sm-8">
                        <select name="selcidaded" id="selCidaded" class="select-basic form-control" style="width: 100%;" onchange="mostrarbairrod()" >
                          <option value=''>Escolha um Estado Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Bairro:</label>
                      <div class="col-sm-8">
                        <select name="selbairrod" id="selBairrod" class="select-basic form-control" style="width: 100%;" >
                          <option value=''>Escolha uma Cidade Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Logradouro:</label>
                      <div class="col-sm-8">
                        <input type="text" name="txtlogradourod" id="logradourod" class="form-control" placeholder="Marques de Olinda" maxlength="45">
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Número:</label>
                      <div class="col-sm-8">
                        <input type="text" name="txtnumerod" id="numerod" class="form-control" maxlength="6" placeholder="1222" onclick="descricaoDestinoObrg()">
                      </div>
                    </div>
                  </div>
                  <!--/. div col-md-12 col-lg-6  -->
                  <div class="form-group row col-lg-12">
                    <label class="col-sm-2 form-control-label">Comentário / Complemento:</label>
                    <div class="col-sm-10">
                      <textarea type"text" name="txtacomplementod" id="complementod" class="form-control" placeholder="Comentário/Complemento do endereço do Destinatário..." ></textarea>
                      <div class="help-block with-errors" style="color: rgb(217, 65, 65);">Necessário caso logradouro e número não estejam preenchidos!</div>
                    </div>
                  </div>
              </div>
              </div>
            </div>
          </div>
  • He is saying that $('[name="rdDestinaIdemReme"]:checked').val(); not found on page

  • Yes only that this on the page even the function comes to work and do what has to do, which in case is to copy the data of other inputs that have already been filled

  • So the code works? Dude, I don’t understand

  • Yes the code works only that gives as the variable undefined

  • The only thing that can be is that it is not finding this radio, you have already looked in this file ff_admin.php on line 806?

  • yes , nothing wrong, :c, I looked at other sites, and said it was something related to global variable, but I could not solve!

  • It gets a little difficult to solve without the full code :/ this is a kind of problem that really needs to be studied as a whole

  • I’ll post the whole page then

Show 3 more comments

1 answer

1


you are trying to access a variable that does not exist in your current scope.

you are to declare rdIdem inside function rdDesIdemRem(), so it is only accessible within this function.

when trying to access it within the event change of input[type=radio] is still not available, so its value is null.

then the solution to your problem, is to declare it again within the scope of change

$(document).on("change","input[type=radio]",function(){
  var rdRetirar = $('[name="rdRetirar"]:checked').val();
  var rdIdem = $('[name="rdDestinaIdemReme"]:checked').val();
  // restante do codigo aqui.
}
  • So it really worked and stopped bugging! Thank you

Browser other questions tagged

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