Function with if Javascript

Asked

Viewed 155 times

0

Good afternoon, I have a function in javascript, and it does not enter the IF, the field txtTipodePlano is filled as "MONTHLY", it should enter, only it falls in Else, follows as I am doing:

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 the txt is:

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

It enters right in ckPosPago, only it doesn’t enter t == "MONTHLY". I have researched, changed several times, but none it enters the IF, I thank.

1 answer

2


The problem is here:

document.getElementById('txtTipodePlano').value;

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

To correct, change this line:

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

for that:

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

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

  • Artur, it worked, he enters the if, only now I have a problem passing the value to a textbox, he informs that is null. Could help me?

Browser other questions tagged

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