Function with if JavaScript

0

Good afternoon, I have a function in javascript, and it does not enter the IF, the txtTipodePlano field is filled in as "MONTHLY", it should come in, just that it falls on the else,

if (document.getElementById("<%=ckPosPago.ClientID%>").checked) {
  var t = document.getElementById('txtTipodePlano').value;
  if (t == "MENSAL") {
    now = new Date;
    var dia_atual = now.getDate();
    var atual_data = toDate(document.getElementById("<%= txtDataInicio.ClientID %>").value);
    var dia_escolha = document.getElementById("<%= txtDiaVencimento.ClientID %>").value;

    function toDate(atual_data) {
      let partes = atual_data.split('/');
      return new Date(partes[2], partes[1], partes[0]);
    }
    if (parseInt(dia_escolha) > parseInt(dia_atual)) {
      var data_tolerancia;
      var total_dias = dia_escolha - dia_atual;
      atual_data.setDate(atual_data.getDate() + parseInt(total_dias));
      document.getElementById("<%= txtVencimentoC.ClientID %>").value = atual_data.format("dd/MM/yyyy HH:mm:ss");
      var tol = (document.getElementById("<%= txtToleranca.ClientID %>").value);
      atual_data.setDate(atual_data.getDate() + parseInt(tol));
      document.getElementById("<%= txtDataTolerancia.ClientID %>").value = atual_data.format("dd/MM/yyyy HH:mm:ss");
    } else {
      var data_tolerancia;
      var total_dias = dia_escolha - dia_atual;
      atual_data.setDate(atual_data.getDate() + parseInt(total_dias));
      document.getElementById("<%= txtVencimentoC.ClientID %>").value = atual_data.format("dd/MM/yyyy HH:mm:ss");
      var tol = (document.getElementById("<%= txtToleranca.ClientID %>").value);
      atual_data.setDate(atual_data.getDate() + parseInt(tol));
      document.getElementById("<%= txtDataTolerancia.ClientID %>").value = atual_data.format("dd/MM/yyyy HH:mm:ss");
    }
  } else {
    function toDate(data) {
      let partes = data.split('/');
      return new Date(partes[2], partes[1] - 1, partes[0]);
    }
    var atual_data1 = toDate(document.getElementById("<%= txtDataFim.ClientID %>").value);
    document.getElementById("<%= txtVencimentoC.ClientID %>").value = atual_data1.format("dd/MM/yyyy HH:mm:ss");
    var tol = (document.getElementById("<%= txtToleranca.ClientID %>").value);
    atual_data1.setDate(atual_data1.getDate() + parseInt(tol));
    document.getElementById("<%= txtDataTolerancia.ClientID %>").value = atual_data1.format("dd/MM/yyyy HH:mm:ss");
    document.getElementById("<%= txtpropor.ClientID %>").value = null;
  }
}

This is how txt is:

<asp:TextBox ID="txtTipodePlano" runat="server" CssClass="form-control"></asp:TextBox>

It goes right in ckPosPago, but it does not enter t == "MONTHLY". I have already researched, changed several times, but none of them enter the IF, thank you.

    
asked by anonymous 06.11.2017 / 19:10

1 answer

2

The problem is here:

document.getElementById('txtTipodePlano').value;

When you create a control with runat="server" , its id will change, unless you have ClientIDMode="Static" . Therefore, he did not find the correct value, and therefore is never equal to "MONTHLY"

To fix, change this line:

var t = document.getElementById('txtTipodePlano').value;

by this:

var t = document.getElementById("<%= txtTipodePlano.ClientID %>").value;

I recommend that you learn to debug your codes, both Javascript and server side. A useful tool is the debugger command. Put it just above the line where you want to debug, and keep the console open, and Javascript execution will stop there

    
06.11.2017 / 19:16