JQuery查找父元素或查找
如何找到此类中名为:。项名称的活动项? 由于这些项目是动态的,我正在使用.find,但我想我需要在这里的某个地方添加一个.parent $this.find'.item name'.valcustomers.product HTML: 更新填充所有产品和说明:JQuery查找父元素或查找,jquery,Jquery,如何找到此类中名为:。项名称的活动项? 由于这些项目是动态的,我正在使用.find,但我想我需要在这里的某个地方添加一个.parent $this.find'.item name'.valcustomers.product HTML: 更新填充所有产品和说明: $('.add-product').click(function(){ //$('.add-product').live('click',function(){ // ***********************************
$('.add-product').click(function(){
//$('.add-product').live('click',function(){
// **************************************
$("#productdiv").css("display", "block");
$.ajax({
url: 'product_fill.php',
data: {action:"invoice"},
dataType: 'json',
success: function(data){
populateSelectBoxes($('#productdiv #ddproduct'), data);
function populateSelectBoxes($select, data) {
var products = [];
$.each(data, function() {
products.push('<li data-value="'+this.autonum+'">' + this.product + '</li>');
});
$select.append(products.join(''));
}
function populateTableRow($addProduct, data, selectedCustomerAutonum) {
var customers;
$.each(data, function() {
if (this.autonum == selectedCustomerAutonum) {
customers = this;
return false;
}
});
$addProduct.parents().find("textarea[name^='item_name']").val(customers.product);
$addProduct.parents('.item-row').find("textarea[name^='item_desc']").val(customers.disc);
$addProduct.parents('.item-row').find("textarea[name^='item_cost']").val(customers.rate);
}
$('#productdiv #ddproduct li').click(function(e) {
var selection = $(this).attr("data-value");
$(this).parent().parent().parent().hide();
populateTableRow($('.add-product'), data, selection);
$('ul').empty();
});
}
});
这些线路:
$(this).find('.item-name').val(customers.product);
$(this).find('.item-desc').val(customers.disc);
正在查找类名为item name和item desc的所有元素,即HTML中没有的元素。您需要将匹配的name属性作为目标
$(this).find("textarea[name^='item-name']").val(customers.product);
$(this).find("textarea[name^='item-desc']").val(customers.disc);
根据您显示的代码很难确定,但是,您可能还需要使用$tableBody.find而不是$this.find,因为在此范围内,$this可能是空的。我的问题是,populateTableRow函数更新元素的方式与单击触发$'的锚点相同。添加产品。'live'click'。。。事件处理程序
解决方案是在单击处理程序的作用域中创建一个变量,并从populateTableRow中引用它
在正确的范围内添加了变量clickedRow:
$('.add-product').live('click',function(){
var clickedRow = $(this).closest("tr"); // <--- ADD THIS PART
$("#productdiv").css("display", "block");
$.ajax({
url: 'product_fill.php',
data: {action:"invoice"},
dataType: 'json',
success: function(data){
populateSelectBoxes($('#productdiv #ddproduct'), data);
function populateSelectBoxes($select, data) {
var products = [];
$.each(data, function() {
products.push('<li data-value="'+this.autonum+'">' + this.product + '</li>');
});
$select.append(products.join(''));
}
function populateTableRow(data, selectedCustomerAutonum) {
var customers;
$.each(data, function() {
if (this.autonum == selectedCustomerAutonum) {
customers = this;
return false;
}
});
clickedRow.find("textarea[name^='item_name']").val(customers.product);
clickedRow.find("textarea[name^='item_desc']").val(customers.disc);
clickedRow.find("textarea[name^='item_cost']").val(customers.rate);
}
$('#productdiv #ddproduct li').click(function(e) {
var selection = $(this).attr("data-value");
$(this).parent().parent().parent().hide();
populateTableRow(data, selection);
$('ul').empty();
});
}
});
.find“.item name”确实会找到所有匹配的子元素。尽管您试图设置td元素的值.val,但它没有值。在这些行中也不清楚这指的是什么,所以您甚至在哪里进行搜索?我正在尝试添加到文本区域,名称=item_name[]确定:$'.item name'.findtextarea[name^='item_name'].valcustomers.product;填充每一行和$this.findtextarea[name^='item_name'].valcustomers.product;什么都不做?如何在代码的其他地方调用populateTableRow?如何引用它,能否显示完整调用,包括参数ie之间的所有内容?运行正常:$'.add product'.parents'.item row'.findtextarea[name^='item_name'].valcustomers.product;本以为在添加到动态行之前它是固定的-现在所有的行都得到选中的单击。-查看原始问题中的更新。我可以查看HTML的其余部分吗?比如,productdiv ddproduct在哪里?
$(this).find("textarea[name^='item-name']").val(customers.product);
$(this).find("textarea[name^='item-desc']").val(customers.disc);
$('.add-product').live('click',function(){
var clickedRow = $(this).closest("tr"); // <--- ADD THIS PART
$("#productdiv").css("display", "block");
$.ajax({
url: 'product_fill.php',
data: {action:"invoice"},
dataType: 'json',
success: function(data){
populateSelectBoxes($('#productdiv #ddproduct'), data);
function populateSelectBoxes($select, data) {
var products = [];
$.each(data, function() {
products.push('<li data-value="'+this.autonum+'">' + this.product + '</li>');
});
$select.append(products.join(''));
}
function populateTableRow(data, selectedCustomerAutonum) {
var customers;
$.each(data, function() {
if (this.autonum == selectedCustomerAutonum) {
customers = this;
return false;
}
});
clickedRow.find("textarea[name^='item_name']").val(customers.product);
clickedRow.find("textarea[name^='item_desc']").val(customers.disc);
clickedRow.find("textarea[name^='item_cost']").val(customers.rate);
}
$('#productdiv #ddproduct li').click(function(e) {
var selection = $(this).attr("data-value");
$(this).parent().parent().parent().hide();
populateTableRow(data, selection);
$('ul').empty();
});
}
});