jQuery 选择器按 ID(包括其他 ID)的问题
我的页面上有两个表:#add 和#items。 #add 有一行输入。 #items 有多行。我正在将输入保存到数据库中。 #add 只应该在单击按钮时保存,而 #items 则在模糊时保存。
问题是 #add 输入也试图节省模糊。我下面使用的选择器正在处理 #add 表输入和 #items 表输入。
这是代码示例(如果需要,我可以链接到源文件):
$("#items tbody tr td input:not('[name=\"ext\"]')").live('focus',function() {
$(this).attr("readonly",false);
$(this).select();
curEdit = $(this).val();
var name = $(this).attr('name');
if (name == 'item') {
$(this).alphanumeric();
} else if (name == 'tag') {
$(this).alphanumeric({allow:" "});
} else if (name == 'desc') {
$(this).alphanumeric({allow:".,-()/ "});
} else if (name == 'qty') {
$(this).numeric();
} else if (name == 'cost' || name == 'list' || name == 'disc' || name == 'unit' || name == 'ext') {
$(this).numeric({allow:"."});
}
}).live('blur',function() {
$(this).attr("readonly",true);
if (curEdit != $(this).val()) { // only update on changes
var col = $(this).parent().prevAll().length;
var query = "action=edit&id="+$(this).parents('tr').attr('rel');
if (col == 1 || col == 10) { $(this).val($(this).val().toUpperCase()); } // format ITEM and GSA
if (col > 4 && col < 10) { $(this).val(formatCurrency($(this).val())); } // format COST, LIST, DISC, UNIT, and EXTENDED
if (col == 3) { $(this).val(ucEach($(this).val())); }
if (col > 3 && col < 10 && col != 5) {
var qty = $(this).parents('tr').find("[name='qty']").val();
var list = $(this).parents('tr').find("[name='list']").val();
var disc = $(this).parents('tr').find("[name='disc']").val();
if ($(this).attr('name') != "unit") {
var unit = formatCurrency(list * (1-(disc/100)));
$(this).parents('tr').find("[name='unit']").val(unit);
} else {
disc = formatCurrency(((list - $(this).val())/list)*100);
$(this).parents('tr').find("[name='disc']").val(disc);
}
unit = $(this).parents('tr').find("[name='unit']").val();
var ext = formatCurrency(qty * unit);
$(this).parents('tr').find("[name='ext']").val(ext);
query = query + "&field[]=qty&val[]="+qty+"&field[]=list&val[]="+list+"&field[]=unit&val[]="+unit+"&field[]=disc&val[]="+disc+"&field[]=ext&val[]="+ext;
} else {
query = query + "&field="+$(this).attr('name')+"&val="+$(this).val();
}
$.post("itemsdb.php", query, function(data) {
if (data.res == "fail") {
alert("There was a problem saving this item."+data.error);
}
},"json");
}
});
I have two tables on my page: #add and #items. #add has a single row of inputs. #items has multiple rows. I'm saving the inputs to a database. #add is only supposed to save on a button click whereas #items saves on blur.
The problem is that the #add inputs are trying to save on blur as well. The selector I'm using below is working on the #add table inputs as well as the #items table input.
Here is a sample of the code (I can link to the source file if need be):
$("#items tbody tr td input:not('[name=\"ext\"]')").live('focus',function() {
$(this).attr("readonly",false);
$(this).select();
curEdit = $(this).val();
var name = $(this).attr('name');
if (name == 'item') {
$(this).alphanumeric();
} else if (name == 'tag') {
$(this).alphanumeric({allow:" "});
} else if (name == 'desc') {
$(this).alphanumeric({allow:".,-()/ "});
} else if (name == 'qty') {
$(this).numeric();
} else if (name == 'cost' || name == 'list' || name == 'disc' || name == 'unit' || name == 'ext') {
$(this).numeric({allow:"."});
}
}).live('blur',function() {
$(this).attr("readonly",true);
if (curEdit != $(this).val()) { // only update on changes
var col = $(this).parent().prevAll().length;
var query = "action=edit&id="+$(this).parents('tr').attr('rel');
if (col == 1 || col == 10) { $(this).val($(this).val().toUpperCase()); } // format ITEM and GSA
if (col > 4 && col < 10) { $(this).val(formatCurrency($(this).val())); } // format COST, LIST, DISC, UNIT, and EXTENDED
if (col == 3) { $(this).val(ucEach($(this).val())); }
if (col > 3 && col < 10 && col != 5) {
var qty = $(this).parents('tr').find("[name='qty']").val();
var list = $(this).parents('tr').find("[name='list']").val();
var disc = $(this).parents('tr').find("[name='disc']").val();
if ($(this).attr('name') != "unit") {
var unit = formatCurrency(list * (1-(disc/100)));
$(this).parents('tr').find("[name='unit']").val(unit);
} else {
disc = formatCurrency(((list - $(this).val())/list)*100);
$(this).parents('tr').find("[name='disc']").val(disc);
}
unit = $(this).parents('tr').find("[name='unit']").val();
var ext = formatCurrency(qty * unit);
$(this).parents('tr').find("[name='ext']").val(ext);
query = query + "&field[]=qty&val[]="+qty+"&field[]=list&val[]="+list+"&field[]=unit&val[]="+unit+"&field[]=disc&val[]="+disc+"&field[]=ext&val[]="+ext;
} else {
query = query + "&field="+$(this).attr('name')+"&val="+$(this).val();
}
$.post("itemsdb.php", query, function(data) {
if (data.res == "fail") {
alert("There was a problem saving this item."+data.error);
}
},"json");
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的选择器可能选择了不正确的元素。尝试在浏览器的 Javascript 控制台中运行选择器:
然后,检查返回的对象并确保它们与您尝试匹配的对象匹配。
It sounds like your selector might be picking up incorrect elements. Try running your selector in the Javascript console of your browser:
Then, examine the object(s) that are returned and ensure that they match what you're trying to match.