jquery 中的 parseFloat 没有选择每个小数值
parseFloat in jquery is not picking each decimal value
<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>
在上面table,
每个 table 行有 4 td,
选择与每个 tr 关联的按钮时,该行的 td 值应为 selected/stored
但是
像
这样的值
43.5 , 0.088 , 0.0034 ;
42.5 , 0.095 , 0.0038 ;
41.5 , ... ;
40.5 , ... ;
39.5 , ... ;
38.5 , ... ;
its shows "Nan"
对于 TR 的其余部分,它显示了确切的值,
我的要求是选择值并通过 AJAX 将它们发送到数据库以备更新
您有许多带有 space - 38 kacha
等的 ID,它们在您的点击函数中没有计算出正确的选择器。
解决此问题的一种方法是使用破折号分隔的 ID 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>
<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>
在上面table, 每个 table 行有 4 td, 选择与每个 tr 关联的按钮时,该行的 td 值应为 selected/stored
但是 像
这样的值43.5 , 0.088 , 0.0034 ;
42.5 , 0.095 , 0.0038 ;
41.5 , ... ;
40.5 , ... ;
39.5 , ... ;
38.5 , ... ;
its shows "Nan"
对于 TR 的其余部分,它显示了确切的值,
我的要求是选择值并通过 AJAX 将它们发送到数据库以备更新
您有许多带有 space - 38 kacha
等的 ID,它们在您的点击函数中没有计算出正确的选择器。
解决此问题的一种方法是使用破折号分隔的 ID 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>