【问题标题】:Problem with jQuery selector by ID including other IDsjQuery选择器的问题,包括其他ID
【发布时间】:2010-12-01 18:11:58
【问题描述】:

我的页面上有两个表格:#add 和#items。 #add 有单行输入。 #it​​ems 有多行。我正在将输入保存到数据库中。 #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");
    }
});

【问题讨论】:

  • 不看你的html很难说。
  • 你的表ID是#add表#items的子表吗?另外,您是否可能有另一个 ID 为 #items 的封装元素?

标签: jquery jquery-selectors css-selectors


【解决方案1】:

听起来您的选择器可能选择了不正确的元素。尝试在浏览器的 Javascript 控制台中运行选择器:

$("#items tbody tr td input:not('[name=\"ext\"]')")

然后,检查返回的对象并确保它们与您要匹配的对象匹配。

【讨论】:

  • 我将选择器放入 firebug 并注意到它包含了表父 div 中的所有内容。在这和上面基思的评论之间,我注意到父 div 也是#items。我不敢相信我错过了。感谢所有的帮助。
  • 很高兴为您提供帮助!请务必单击解决您的问题的答案旁边的绿色复选标记。欢迎使用 Stack Overflow!
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
相关资源
最近更新 更多