我正在尝试将javascript函数放入其中,但我遇到了一些麻烦。
HTML
<table class="table table-borderless table-hover table-responsive">
<thead class="thead-inverse">
<tr>
<th>Order Number</th>
<th>Company Name</th>
<th>Symbol</th>
<th>Bought Price</th>
<th>Current Price</th>
<th>Shares</th>
<th>Profit</th>
<th>Type of Stock</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
while($transarray = mysqli_fetch_array($_SESSION['trans'])){
echo
"<tr>
<td>".'trans_num'."</td>
<td>".'stock_name'."</td>
<td>".'stock_ticker'."</td>
<td>". 'stock_bp'."</td>
<td class='stockprofit'>"."."."</td>
<td>".'stock_shares'."</td>
<td>".'stock_change'."</td>
<td>".'stock_type'."</td>
<td><button class='sellButton btn btn-primary' data-target='#modalsell' id='sellStock'>Sell Stock</button></td>
</tr>";
}
?>
</tbody>
</table>
有了这个,我试图将当前传递给函数并找到stock_ticker单元格的位置来执行其他一些功能。但是当我尝试使用 find(“td:nth-child(3)”)时,自动收报机是一个空白对象.text()。这可能是因为在调用函数之前表还没有完全完成处理吗?
我尝试过使用<td onload='function(stock_sticker)'>...</td>
,但是当我在函数中放置警报来测试它时,它无效。
$(document).ready(function(){
$('.stockprofit').each(function({$(this).html(getPrices($(this)))}));
});
function getPrices(e){
return 'test';
}
我的桌子上什么都没有
答案 0 :(得分:1)
你的$(this)指的是当前的dom元素。像each()这样的jQuery函数允许你传递一个接收当前元素的函数。
<table class="table table-borderless table-hover table-responsive">
<thead class="thead-inverse">
<tr>
<th>Order Number</th>
<th>Company Name</th>
<th>Symbol</th>
<th>Bought Price</th>
<th>Current Price</th>
<th>Shares</th>
<th>Profit</th>
<th>Type of Stock</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
echo
"<tr>
<td>".'trans_num'."</td>
<td>".'stock_name'."</td>
<td>".'stock_ticker'."</td>
<td>". 'stock_bp'."</td>
<td class='stockprofit'>"."."."</td>
<td>".'stock_shares'."</td>
<td>".'stock_change'."</td>
<td>".'stock_type'."</td>
<td><button class='sellButton btn btn-primary' data-target='#modalsell' id='sellStock'>Sell Stock</button></td>
</tr>";
?>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('.stockprofit').each(function(){$(this).html(getPrices($(this)));
});
$('#sellStock').click( function(){
});
});
var child = 0;
function getPrices(e){
console.log(e);
child++;console.log(child);
console.log(e.closest('tr').find("td:nth-child("+child+")").text());
var ticker = e.closest('tr').find("td:nth-child(3)").text();
//alert(ticker);
return ticker;
}
</script>