jQuery在双引号内获取输入值

时间:2017-02-28 21:51:58

标签: jquery

我有以下代码示例,我想每次都获取cell1的值。

$(document).ready(function() {
  var counter = 1;
  var str1 = 'Items';
  var tmx;
  var tmx2;
  var temaxia;
  $("#dosomething").click(function() {
    var table = document.getElementById("myTable");
    var row = table.insertRow();

    var cell0 = row.insertCell(0);
    var cell1 = row.insertCell(1);
    var cell2 = row.insertCell(2);
    var cell3 = row.insertCell(3);
    var cell4 = row.insertCell(4);
    var cell5 = row.insertCell(5);
    cell0.innerHTML = counter;

    tmx = str1.concat(counter);
    tmx2 = "#".concat(str1).concat(counter);
    cell1.innerHTML = "<input name='" + tmx + "' id='" + tmx + "'>";

    temaxia = $("#Items".concat(counter)).val();
    alert(temaxia);
    counter = counter + 1;
  });

});
<html>

<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>

<body>

  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-12">
        <h3>Παραγγελία Αναλωσίμων</h3>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <table style="width:100%" class="table table-striped table-bordered" id="myTable">
          <thead>
            <tr>
              <th>Α/Α</th>
              <th>ΤΜΧ</th>
              <th>ΤΙΜΗ ΜΟΝ</th>
              <th>ΜΕΡ ΣΥΝΟΛΟ</th>
              <th>ΦΠΑ</th>
              <th>ΤΙΜΗ ΜΕ ΦΠΑ</th>
              <!--th><a id="plus" href="#"><img src="img/plus.png" alt="plus" height="32" width="32"></a></th-->
              <th><button id="dosomething">OK</button></th>
            </tr>
          </thead>
          <tbody>
            <tr></tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</body>

</html>

如何获取cell1的值?我尝试了几种方法,但都没有,例如: temaxia = $(“#Items”.concat(counter))。val(); temaxia = $(tmx2).val();

2 个答案:

答案 0 :(得分:1)

即使你的代码有效,你也总是得到一个空值,因为你刚刚创建了输入。

我认为你想要的是在有人在其中加入值时获取输入的值。

所以我改变了你的代码并创建了一个可以解决你的问题的JSFiddle示例。获得想法,您可以改变和改进以满足您的需求。

&#13;
&#13;
$(document).ready(function() {
    var counter = 1;
    var str1 = 'Items';
    var tmx;
    var tmx2;
    var temaxia;
    
    var table = document.getElementById("myTable");

    $("#dosomething").click(function() { 
        var row = table.insertRow();

        var cell0 = row.insertCell(0);
        var cell1 = row.insertCell(1);
        var cell2 = row.insertCell(2);
        var cell3 = row.insertCell(3);
        var cell4 = row.insertCell(4);
        var cell5 = row.insertCell(5);
        var cell6 = row.insertCell(6);
        
        cell0.innerHTML = counter;

        tmx = str1.concat(counter);
        
        cell1.innerHTML = "<input name='"+tmx+"' id='"+tmx+"'>";
        cell6.innerHTML = "<button onclick=\"alert($('#" + tmx + "').val());\">OK</button>";

        counter++;
    });
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <h3>Παραγγελία Αναλωσίμων</h3>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <table style="width:100%" class="table table-striped table-bordered" id="myTable">
                <thead>
                    <tr>
                    <th>Α/Α</th>
                    <th>ΤΜΧ</th>
                    <th>ΤΙΜΗ ΜΟΝ</th>
                    <th>ΜΕΡ ΣΥΝΟΛΟ</th>
                    <th>ΦΠΑ</th>
                    <th>ΤΙΜΗ ΜΕ ΦΠΑ</th>
                    <!--th><a id="plus" href="#"><img src="img/plus.png" alt="plus" height="32" width="32"></a></th-->
                    <th><button id="dosomething">OK</button></th>
                    </tr>
                </thead>
                <tbody>
                    <tr></tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

在此处查看:https://jsfiddle.net/pg4rjnzg/1/

答案 1 :(得分:0)

这是你需要的吗?

&#13;
&#13;
$(document).ready(function() {
  var counter = 1;
  var str1 = 'Items';
  var tmx;
  var tmx2;
  var temaxia;
  $("#dosomething").click(function() {
    var table = document.getElementById("myTable");
    var row = table.insertRow();

    var cell0 = row.insertCell(0);
    var cell1 = row.insertCell(1);
    var cell2 = row.insertCell(2);
    var cell3 = row.insertCell(3);
    var cell4 = row.insertCell(4);
    var cell5 = row.insertCell(5);
    cell0.innerHTML = counter;

    tmx = str1.concat(counter);
    tmx2 = "#".concat(str1).concat(counter);
    cell1.innerHTML = "<input name='" + tmx + "' id='" + tmx + "'>";

    temaxia = $(tmx2).val();
    counter = counter + 1;
  });

});
&#13;
<html>

<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>

<body>

  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-12">
        <h3>Παραγγελία Αναλωσίμων</h3>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <table style="width:100%" class="table table-striped table-bordered" id="myTable">
          <thead>
            <tr>
              <th>Α/Α</th>
              <th>ΤΜΧ</th>
              <th>ΤΙΜΗ ΜΟΝ</th>
              <th>ΜΕΡ ΣΥΝΟΛΟ</th>
              <th>ΦΠΑ</th>
              <th>ΤΙΜΗ ΜΕ ΦΠΑ</th>
              <!--th><a id="plus" href="#"><img src="img/plus.png" alt="plus" height="32" width="32"></a></th-->
              <th><button id="dosomething">OK</button></th>
            </tr>
          </thead>
          <tbody>
            <tr></tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;