简体   繁体   中英

How to set Javascript's variable value inside <input value=''">

I do not know how to formulate this question but it is near close to my issue. I am trying to make a Point of sale system so when client clicks in each product it adds in the table. There are no issues in my code but I do not know how to set the product's price using a textbox when using .append .

This is my code:

$(".agregarListaF").click(function (e) {
  e.preventDefault();
  var padre = $(this);
  var hijos = padre.children().children("span");
  var i = 0;
  var datos = new Array();

  hijos.each(function () {
    switch (i) {
      case 0:
        datos[0] = $(this).text();
        break;

      case 1:
        padre;
        datos[1] = $(this).text();
        break;

      case 2:
        padre;
        datos[2] = $(this).text();
        break;
    }

    i++;
  });
  console.log(datos[0]);
  console.log(datos[1]);

  $(".detallefactura").append(
    "<tr>" +
      "<td>" +
      datos[0] +
      "</td>" +
      "<td>" +
      datos[1] +
      "</td>" +
      "<td>" +
      "<input type='text' class='form-control text-center' value='1'>" +
      "</td>" +
      "<td>" +
      "<input type='text' class='form-control text-center' value=''>" +
      "</td>" +
      "<td class='tota-sub-fila'>" +
      parseFloat(Math.floor(datos[2]) * parseFloat(1)).toFixed(2) +
      "</td>"
  );
});

My issue is in the line:

 + "<td>" + "<input type='text' class='form-control text-center' value=''>" + "</td>"

I want to add javascript value variable inside * input value=''* but I know It wont work but I tried it:

+ "<td>" + "<input type='text' class='form-control text-center' value='datos[2]'>" + "</td>"

This is my point of sale view: 在此处输入图像描述

In the column precio is where I want to set the price inside that textbox.

How do I solve this?

Please try this code,To How to set Javascript's variable value inside

<!DOCTYPE html> 
<html> 

<head> 
    <title> 
        JavaScript| Set the value of an input field. 
    </title> 
</head> 

<body style="text-align:center;" id="body"> 
    <input type='text' id='id1' /> 
    <br> 
    <br> 
    <button onclick="gfg_Run()"> 
        click to set 
    </button> 
    <p id="GFG_DOWN" style="color:green; font-size: 20px;font-weight: bold;"></p> 

    <script> 
        var el_down = document.getElementById("GFG_DOWN"); 
        var inputF = document.getElementById("id1"); 
        function gfg_Run() { 
            inputF.setAttribute('value', 'defaultValue'); 
            el_down.innerHTML = "Value = " + "'" + inputF.value + "'"; 
        } 
    </script> 
</body> 
</html> 

I hope this information will be useful.

Thank you.

You can do:

var variable="x";

"<input type='text' class='form-control text-center' value='"+ variable +"'>";

Or,
`<input type='text' class='form-control text-center' value='${variable}'>`

Follow this using template literals

 const html = `<tr> 
    <td>${datos[0]}</td> 
    <td>${datos[1]}</td> 
    <td><input type="text" class="form-control text-center" value="${datos[3]}"></td>
    <td><input type="text" class="form-control text-center" value="${datos[4]}"</td> 
    <td class="tota-sub-fila">${parseFloat((Math.floor(datos[2])) * parseFloat(1)).toFixed(2)}</td>
 </tr>`;

$(.detallefactura").append(html);

you can use Template literals like this instead of having this concatenation:

$(.detallefactura").append(`<tr>
    <td> ${datos[0]} </td>
    <td> ${datos[1]} </td>
    <td> <input type='text' class='form-control text-center' value='1'> </td>
    <td> <input type='text' class='form-control text-center' value='${datos[2]}'> </td>
    <td class='tota-sub-fila'> ${parseFloat((Math.floor(datos[2])) * parseFloat(1)).toFixed(2)} </td>
`); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM