针对这个和类的jQuery
我试图从表中提取数据,更确切地说是从列中的类中提取数据针对这个和类的jQuery,jquery,Jquery,我试图从表中提取数据,更确切地说是从列中的类中提取数据 value = jQuery(this).closest('td').prev('td').prev('td').text(); 在这个单元中,有两个类。 其中一个是: .amount 还有一个叫: .currency 我要把目标定在一个叫。金额 我尝试了以下代码,但它给出了金额和货币 value = jQuery(this).closest('td').prev('td').prev('td').find('.amount').
value = jQuery(this).closest('td').prev('td').prev('td').text();
在这个单元中,有两个类。
其中一个是:
.amount
还有一个叫:
.currency
我要把目标定在一个叫。金额
我尝试了以下代码,但它给出了金额和货币
value = jQuery(this).closest('td').prev('td').prev('td').find('.amount').text();
我做错了什么
jQuery
jQuery('input').on('click', function() {
if (jQuery(this).is(':checked')) {
productName = jQuery(this).closest('td').prev('td').text();
value = jQuery(this).closest('td').prev('td').prev('td.amount').text();
jQuery(".spiseseddel ul").append('<li>' + productName + value + '</li>');
} else {
productName = jQuery(this).closest('td').prev('td').text();
jQuery('.spiseseddel ul li').filter(function() {
return jQuery.text([this]) === productName;
}).remove();
}
})
jQuery('input')。在('click',function()上{
if(jQuery(this).is(':checked')){
productName=jQuery(this).closest('td').prev('td').text();
value=jQuery(this).closest('td').prev('td').prev('td.amount').text();
jQuery(“.spiseeddel ul”).append(“”+productName+value+” );
}否则{
productName=jQuery(this).closest('td').prev('td').text();
jQuery('.spiseeddel ul li').filter(函数(){
返回jQuery.text([this])==productName;
}).remove();
}
})
HTML
<tr>
<td>
<span class="amount">25,00
<span class="currency">DKK</span>
</span>
</td>
<td class="col-name">product name
</td>
<td class="add-to-cart">
<input type="checkbox">
</td>
</tr>
25,00
DKK
产品名称
在要定位的单元格中使用.find()
,而不是在当前单元格中
value = jQuery(this).closest('td').prev('td').prev('td').find(".amount").text();
只需指定您需要的td类:
value = jQuery(this).closest('td').prev('td').prev('td.amount').text();
请发布您想与此一起使用的HTML。哪个元素是
此
?我已经添加了HTML,对此表示抱歉。此
将不是当前单元格,否则最近('td')
可能不会返回任何内容(除非它是表中的表)。我假设此
是当前单元格中的某个内容,如按钮,复选框或链接。他说.amount
和.currency
在单元格内,而不是单元格本身。.prev('td.amount')
不搜索具有该类的td。如果与选择器td.amount
匹配,则返回上一个元素,否则不返回任何内容。已更新问题。抱歉搞混了。