JQuery有趣的问题

JQuery有趣的问题,jquery,ajax,Jquery,Ajax,这是我的一段代码: ................. remProduct: function(e, $a) { var orderObj = { pid: $a.parents('li').find('input=[name=pid]').val(), pk_id: $a.parents('div.packet').attr('id') || $a.parents('div.section').attr('id'),

这是我的一段代码:

.................
 remProduct: function(e, $a) {
        var orderObj = {
            pid:   $a.parents('li').find('input=[name=pid]').val(),
            pk_id: $a.parents('div.packet').attr('id') || $a.parents('div.section').attr('id'),
            isSideCart: !!$a.parents('div.packet').length
        };
        var callBack = function(json) {
            if (json.success) { 
                if ($("div.sidebar").length) {
                    $("div.sidebar").replaceWith(json.body)
                } else if ($("div.content").length) {
                    $("div.content").html(json.body);
                }
            }
            front.rebindSideCart();
        };
        $.post(this.getPath() + '/remove-product', orderObj, callBack, 'json');
    },
.................
我的想法是从我的软件包中添加或删除产品。当我添加一个产品,然后我想删除它时,它不工作,firebug向我显示以下错误:

错误:语法错误,无法识别的表达式:input=[name=pid]


但是如果我刷新页面,它会正常工作。谁能解释一下原因吗?提前谢谢

您不需要在
input
之后立即在
input=[name=pid]
中使用
=

pid: $a.parents('li').find('input[name=pid]').val(),
的正确语法是:
jQuery(“[attribute='value']”)


由于您的值只是一个普通字符串,因此不需要将其括在双引号内,但是如果您的值包含任何元字符(例如
!“\$%&'()*+,./:;?@[\]^
{124;}`)作为名称的文字部分,则必须用两个反斜杠转义:\\或用引号括起来。

输入之后不需要
=
运算符。还要用引号括起属性值。它应该是:

find('input[name="pid"]')
pid:


它应该是
input[name=pid]
not
input=[name=pid]

pid:   $a.parents('li').find('input[name=pid]').val(),
                   //--------------^ 
                   // remove = from this position as it is not a valid selector
语法:

$("element[attribute='value']")
$("input[name='pid']")
对于egs:

$("element[attribute='value']")
$("input[name='pid']")

thx man,我没有意识到这是“=”。我会在不到10分钟内接受你的答案。可能是如果把你的代码放在
文档就绪
onload
函数中,它应该会在每页
刷新
时触发
错误
。否则,它只会在该语句执行时触发。