开发者

I can't call the function in prototype .js. Where am I making a mistake?

开发者 https://www.devze.com 2022-12-25 16:21 出处:网络
Code: DeCheBX = $(\'MyDiv\').insert(new Element(\'input\', { \'type\': \'checkbox\', \'id\': \"Img\" + obj[i].Nam, \'value\': obj[i].IM, \'onClick\': SayHi(id)}));

Code:

DeCheBX = $('MyDiv').insert(new Element('input', { 'type': 'checkbox', 'id': "Img" + obj[i].Nam, 'value': obj[i].IM, 'onClick': SayHi(id)           }));
                            document.body.appendChild(DeCheBX);
                            DeImg = $('MyDiv').insert(new Element('img', { 'id': "Imgx" + obj[i].Nam, 'src': obj[i].IM }));
                            document.body.appendChild(DeImg);
                        }
                        SayHi = function(x)开发者_StackOverflow {
                            try {

                            if($('x').checked == true)
                            {
                                alert("press" + x);
                            }

                    }
                    catch (e) {
                        alert("error");
                    }

                };


1: You should declare your variables DeCheBX, DeImg and SayHi with the var keyword.
2: In the example it is unclear where obj[i] comes from and what it is. Errors may stem from it being not what you hope.
3: You most certanly don't want to call the function SayHi and assign its return value (undefined) to the onclick handler of the Element you construct and insert into 'MyDiv'. The way you are doing things, It'd better be a string like "SayHi(this);".
4: There is an extra } after document.body.appendChild(DeImg);. It makes this part of your example code invalid.
5: In the function SayHi you look at the checked property of an element with id 'x'. It seems to me that you'd rather want to use the value of the x passed to the function (see 3 above).

0

精彩评论

暂无评论...
验证码 换一张
取 消