您如何找到正确的方法来进行此比较?我尝试了各种方法。我甚至使用过ID,但他们没有回应。如果我这样做“gumboots”字符串检查,它确实有效。 “gumboots”只是表格中存在的产品名称的字符串值。这就是我知道我根本不需要PHP的方法,尽管下面的索引视图中的PHP显示了表格。任何的想法?我会很感激。
这是javascript
$('#example tbody tr td').each(function()
{
var p_no_in_stock = parseInt(document.getElementById('p_no_in_stock')).value;
var p_reorder_quantity = parseInt(document.getElementById('p_reorder_quantity')).value;
//if ($product['Product']['p_no_in_stock'].val() < $product['Product']['p_reorder_quantity'].val())
if ($(this).text() == "gumboots")
//if ($(this).p_no_in_stock < $(this).p_reorder_quantity)
{
//$("#row_" +" td").effect("highlight", {}, 1500);
$(this).parent().attr('style','background:red');
$(this).parent().css('font-weight','bold');
}
});
这是名为Products.index的视图中的应用程序
<div class="active">
<h2><?php echo __('Products'); ?></h2>
<table cellpadding="0" cellspacing="0" class="table table-striped table-bordered" id ="example">
<tr>
<th><?php echo $this->Paginator->sort('p_name', 'Name'); ?></th>
<th><?php echo $this->Paginator->sort('category_name', 'Category'); ?></th>
<th><?php echo $this->Paginator->sort('p_no_in_stock','No. in Stock'); ?></th>
<th><?php echo $this->Paginator->sort('p_reorder_quantity', 'Re-order Quantity'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<td><?php echo h($product['Product']['p_name']); ?></td>
<td> <?php echo $this->Html->link($product['Category']['category_name'],
array('controller' => 'categories', 'action' => 'view', $product['Category']['id'])); ?>
</td>
<td id = "p_no_in_stock" type ="number" ><?php echo h($product['Product']['p_no_in_stock']); ?> </td>
<td id ="p_reorder_quantity" type ="number" ><?php echo h($product['Product']['p_reorder_quantity']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $product['Product']['id']), array('class' => 'btn btn-mini')); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $product['Product']['id']), array('class' => 'btn btn-mini'), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
提前致谢。
已解决的问题
删除类型编号
将tr标签编辑为
<tr class ="item-row"> after foreach part.
答案 0 :(得分:0)
首先,您需要改变一些事项:
type="number"
属性。 type-attribute仅适用于input-tags 看一下这个小提琴:http://jsfiddle.net/FTh4m/看一个正在运行的代码(假设你在页面上有jQuery)。
如果您对jQuery不太熟悉,我可以解释每行的内容。
至于为什么你现在所拥有的东西不起作用...这个解释相当长,但我会尽力解释:
你拥有的选择($('#example tbody tr td')
)将在屏幕上返回一个ALL td&#39。
当你执行document.getElementById()
时,它只会获取它在DOM中找到的该ID的第一个实例(我认为IE实际上是自下而上搜索,但那是另一天的故事)。
所以在每个循环中,你实际上是在浏览页面上的每一个td,而不仅仅是你关心的td。
未来的专业提示,在尝试调试此类客户端JS时,打开Chrome Dev Tools(或FireBug,如果您是FireFox类型的人)并开始在控制台中测试你的jQuery选择器。