-2

Below is my jQuery Code, I have to change the attribute:

I have 2 list of similar items, i have to disable selected item of first in second list, and in first list i have prefix lstrYM with Item and in second list having prefix ltrY2 with item

I am very week in jquery , so i dont know how exact find, so i am runnig the loop

 $('#lsltrYMaxis').change(function () {
     var flg = 0;
     $('#lsltrYMaxis option').each(function () {
         var val = this.value;
         val = val.replace("YMa", "Y2a"); // find y2 axis with similar index
         var chk = $(this).is(':checked'); // checked 
         //            alert(val);
         //            alert(chk);
         $('#lsltrY2axis option').each(function () {
            var val2 = this.value;
            alert(val + ";" + val2 + ";" + chk);
            if (val === val2) {
                if (chk === true) {
                    alert((this).value);
                    $(this).attr({ 'disabled': true, 'aria-disabled': true, 'checked': false, 'aria-selected': false }).addClass('ui-state-disabled');
                    alert('done');
                    flg = 1;
                    return false;
                }
                else {
                    $(this).attr({ 'disabled': false, 'aria-disabled': false }).removeClass('ui-state-disabled');
                    flg = 1;
                    return false;

                }
            }

        });
        if (flg == 1)
            return false;
    });


});

But it says

(this).attr is not a function

Its applying the attribute

What am I doing wrong?

4

4 回答 4

1

内部.each()回调this是指没有该.attr()方法的dom对象-它由jQuery提供,因此您需要获取元素的jQuery包装器,因此您需要使用$(this).attr()

$('#lsltrY2axis option').each(function () {
    var val2 = this.value;
    alert(val + ";" + val2 + ";" + chk);
    if (val == val2 && chk == true) {
        $(this).attr({ 'disabled': true, 'aria-disabled': true, 'checked': false, 'aria-selected': false }).addClass('ui-state-disabled');
    }
    else {
        $(this).attr({ 'disabled': false, 'aria-disabled': false }).removeClass('ui-state-disabled');
    }
});
于 2013-09-17T07:19:29.440 回答
1

利用

$(this).attr([attribute is here]);

usingthis指的是 DOM。

此外,您必须将所有this出现的地方替换为$(this)

于 2013-09-17T07:21:43.133 回答
1

I have changed this to $(this)
then == to ===
Hope it works

$('#lsltrY2axis option').each(function () {
                var val2 = $(this).value;
                alert(val + ";" + val2 + ";" + chk);
                if (val === val2 && chk === true) {
                    $(this).attr({ 'disabled': true, 'aria-disabled': true, 'checked': false, 'aria-selected': false }).addClass('ui-state-disabled');
                }
                else {
                    $(this).attr({ 'disabled': false, 'aria-disabled': false }).removeClass('ui-state-disabled');
                }

            });
于 2013-09-17T07:22:31.567 回答
0

你应该写:

$(this).attr
于 2013-09-17T07:23:34.877 回答