parseFloat в jquery не выбирает каждое десятичное значение - PullRequest
0 голосов
/ 29 марта 2020

    <table style="width:40%; float: left">
        <tr>
            <th>SWG</th>
            <th>Size</th>
            <th>Size in MM</th>
            <th>Size in INCH</th>
            <th style="display:"></th>
        </tr>
<tr id="43 kacha">
            <td contenteditable>43.5</td>
            <td contenteditable>43 kacha</td>
            <td contenteditable>0.088</td>
            <td contenteditable>0.0034</td>
            <td><button id="edit_entry" value="43 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="43">
            <td contenteditable>43.0</td>
            <td contenteditable>43</td>
            <td contenteditable>0.092</td>
            <td contenteditable>0.0036</td>
            <td><button id="edit_entry" value="43"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="42 kacha">
            <td contenteditable>42.5</td>
            <td contenteditable>42 kacha</td>
            <td contenteditable>0.095</td>
            <td contenteditable>0.0038</td>
            <td><button id="edit_entry" value="42 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="42">
            <td contenteditable>42.0</td>
            <td contenteditable>42</td>
            <td contenteditable>0.102</td>
            <td contenteditable>0.0040</td>
            <td><button id="edit_entry" value="42"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="41 kacha">
            <td contenteditable>41.5</td>
            <td contenteditable>41 kacha</td>
            <td contenteditable>0.107</td>
            <td contenteditable>0.0042</td>
            <td><button id="edit_entry" value="41 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="40 kacha">
            <td contenteditable>40.5</td>
            <td contenteditable>40 kacha</td>
            <td contenteditable>0.117</td>
            <td contenteditable>0.0046</td>
            <td><button id="edit_entry" value="40 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="40">
            <td contenteditable>40.0</td>
            <td contenteditable>40</td>
            <td contenteditable>0.122</td>
            <td contenteditable>0.0044</td>
            <td><button id="edit_entry" value="40"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="39 kacha">
            <td contenteditable>39.5</td>
            <td contenteditable>39 kacha</td>
            <td contenteditable>0.127</td>
            <td contenteditable>0.0050</td>
            <td><button id="edit_entry" value="39 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="39">
            <td contenteditable>39.0</td>
            <td contenteditable>39</td>
            <td contenteditable>0.132</td>
            <td contenteditable>0.0052</td>
            <td><button id="edit_entry" value="39"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="38 kacha">
            <td contenteditable>38.5</td>
            <td contenteditable>38 kacha</td>
            <td contenteditable>0.145</td>
            <td contenteditable>0.0056</td>
            <td><button id="edit_entry" value="38 kacha"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="150">
            <td contenteditable>38.0</td>
            <td contenteditable>150</td>
            <td contenteditable>0.152</td>
            <td contenteditable>0.0060</td>
            <td><button id="edit_entry" value="150"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="180">
            <td contenteditable>37.5</td>
            <td contenteditable>180</td>
            <td contenteditable>0.180</td>
            <td contenteditable>0.0071</td>
            <td><button id="edit_entry" value="180"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="190">
            <td contenteditable>36.0</td>
            <td contenteditable>190</td>
            <td contenteditable>0.190</td>
            <td contenteditable>0.0076</td>
            <td><button id="edit_entry" value="190"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="224">
            <td contenteditable>35.5</td>
            <td contenteditable>224</td>
            <td contenteditable>0.224</td>
            <td contenteditable>0.0088</td>
            <td><button id="edit_entry" value="224"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="215">
            <td contenteditable>35.0</td>
            <td contenteditable>215</td>
            <td contenteditable>0.215</td>
            <td contenteditable>0.0085</td>
            <td><button id="edit_entry" value="215"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="240">
            <td contenteditable>33.5</td>
            <td contenteditable>240</td>
            <td contenteditable>0.240</td>
            <td contenteditable>0.0095</td>
            <td><button id="edit_entry" value="240"><img src="images/icons/edit.png"></button></td>
        </tr><tr id="250">
            <td contenteditable>33.0</td>
            <td contenteditable>250</td>
            <td contenteditable>0.254</td>
            <td contenteditable>0.0100</td>
            <td><button id="edit_entry" value="250"><img src="images/icons/edit.png"></button></td>
        </tr></table>       <span id="err" style="float: left"></span>
        <script>
                $(document).ready(function(){
                    $("button#edit_entry").click(function(){
                        var size = this.value;
                        var id = parseFloat($('tr#'+size+' td').eq(0).text(),10);
                        var size_mm = parseFloat($('tr#'+size+' td').eq(2).text(),10);
                        var size_inch = parseFloat($('tr#'+size+' td').eq(3).text(),10);
                        $("#err").html(id);                                 
                    }); 
                }); 
        </script>

в приведенной выше таблице. Каждая строка таблицы имеет 4 td. При выборе кнопки, связанной с каждым tr, значения td этой строки должны выбираться / сохраняться

, но такие значения, как

43.5 , 0.088 , 0.0034 ; 
42.5 , 0.095 , 0.0038 ;
41.5 , ... ;
40.5 , ... ;
39.5 , ... ;
38.5 , ... ;

its shows "Nan"

, а для остальной части ТР отображаются точные значения,

Мое требование - выбрать значения и отправить их через AJAX в базу данных в случае ОБНОВЛЕНИЯ

1 Ответ

0 голосов
/ 29 марта 2020

У вас есть много идентификаторов с пробелом - 38 kacha et c, которые не дают правильного селектора в вашей функции щелчка.

Один из способов решения этой проблемы - использовать разделенные идентификаторы da sh 38-kacha, как показано ниже:

$(document).ready(function() {
  $("button#edit_entry").click(function() {
    var size = this.value;
    var id = parseFloat($('tr#' + size + ' td').eq(0).text(), 10);
    var size_mm = parseFloat($('tr#' + size + ' td').eq(2).text(), 10);
    var size_inch = parseFloat($('tr#' + size + ' td').eq(3).text(), 10);
    $("#err").html(id);
    
    console.log(size, id, size_mm, size_inch)
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width:100%;">
  <tr>
    <th>SWG</th>
    <th>Size</th>
    <th>Size in MM</th>
    <th>Size in INCH</th>
    <th style="display:"></th>
  </tr>
  <tr id="43-kacha">
    <td contenteditable>43.5</td>
    <td contenteditable>43 kacha</td>
    <td contenteditable>0.088</td>
    <td contenteditable>0.0034</td>
    <td><button id="edit_entry" value="43-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="43">
    <td contenteditable>43.0</td>
    <td contenteditable>43</td>
    <td contenteditable>0.092</td>
    <td contenteditable>0.0036</td>
    <td><button id="edit_entry" value="43"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="42-kacha">
    <td contenteditable>42.5</td>
    <td contenteditable>42 kacha</td>
    <td contenteditable>0.095</td>
    <td contenteditable>0.0038</td>
    <td><button id="edit_entry" value="42-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="42">
    <td contenteditable>42.0</td>
    <td contenteditable>42</td>
    <td contenteditable>0.102</td>
    <td contenteditable>0.0040</td>
    <td><button id="edit_entry" value="42"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="41-kacha">
    <td contenteditable>41.5</td>
    <td contenteditable>41 kacha</td>
    <td contenteditable>0.107</td>
    <td contenteditable>0.0042</td>
    <td><button id="edit_entry" value="41-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="40-kacha">
    <td contenteditable>40.5</td>
    <td contenteditable>40 kacha</td>
    <td contenteditable>0.117</td>
    <td contenteditable>0.0046</td>
    <td><button id="edit_entry" value="40-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="40">
    <td contenteditable>40.0</td>
    <td contenteditable>40</td>
    <td contenteditable>0.122</td>
    <td contenteditable>0.0044</td>
    <td><button id="edit_entry" value="40"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="39-kacha">
    <td contenteditable>39.5</td>
    <td contenteditable>39 kacha</td>
    <td contenteditable>0.127</td>
    <td contenteditable>0.0050</td>
    <td><button id="edit_entry" value="39-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="39">
    <td contenteditable>39.0</td>
    <td contenteditable>39</td>
    <td contenteditable>0.132</td>
    <td contenteditable>0.0052</td>
    <td><button id="edit_entry" value="39"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="38-kacha">
    <td contenteditable>38.5</td>
    <td contenteditable>38 kacha</td>
    <td contenteditable>0.145</td>
    <td contenteditable>0.0056</td>
    <td><button id="edit_entry" value="38-kacha"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="150">
    <td contenteditable>38.0</td>
    <td contenteditable>150</td>
    <td contenteditable>0.152</td>
    <td contenteditable>0.0060</td>
    <td><button id="edit_entry" value="150"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="180">
    <td contenteditable>37.5</td>
    <td contenteditable>180</td>
    <td contenteditable>0.180</td>
    <td contenteditable>0.0071</td>
    <td><button id="edit_entry" value="180"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="190">
    <td contenteditable>36.0</td>
    <td contenteditable>190</td>
    <td contenteditable>0.190</td>
    <td contenteditable>0.0076</td>
    <td><button id="edit_entry" value="190"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="224">
    <td contenteditable>35.5</td>
    <td contenteditable>224</td>
    <td contenteditable>0.224</td>
    <td contenteditable>0.0088</td>
    <td><button id="edit_entry" value="224"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="215">
    <td contenteditable>35.0</td>
    <td contenteditable>215</td>
    <td contenteditable>0.215</td>
    <td contenteditable>0.0085</td>
    <td><button id="edit_entry" value="215"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="240">
    <td contenteditable>33.5</td>
    <td contenteditable>240</td>
    <td contenteditable>0.240</td>
    <td contenteditable>0.0095</td>
    <td><button id="edit_entry" value="240"><img src="images/icons/edit.png"></button></td>
  </tr>
  <tr id="250">
    <td contenteditable>33.0</td>
    <td contenteditable>250</td>
    <td contenteditable>0.254</td>
    <td contenteditable>0.0100</td>
    <td><button id="edit_entry" value="250"><img src="images/icons/edit.png"></button></td>
  </tr>
</table> 
<span id="err"></span>
...