簡體   English   中英

如何在里面設置Javascript的變量值<input value="" ">

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

我不知道如何提出這個問題,但它接近我的問題。 我正在嘗試制作一個銷售點系統,因此當客戶點擊每個產品時,它會添加到表格中。 我的代碼沒有問題,但我不知道在使用.append時如何使用textbox設置產品價格。

這是我的代碼:

$(".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>"
  );
});

我的問題是:

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

我想在* input value=''*中添加 javascript 值變量,但我知道它不起作用但我試過了:

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

這是我的銷售觀點: 在此處輸入圖像描述

precio列中,我想在該文本框中設置價格。

我該如何解決這個問題?

請嘗試這段代碼,如何在里面設置Javascript的變量值

<!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> 

我希望這些信息會有用。

謝謝你。

你可以做:

var variable="x";

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

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

使用模板文字遵循此

 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);

您可以使用這樣的模板文字而不是這樣的連接:

$(.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>
`); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM