未捕获的 ReferenceError:变量未在 onclick 函数 Javascript 上定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20802564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:20:19  来源:igfitidea点击:

Uncaught ReferenceError: variable is not defined on onclick function Javascript

javascriptjquery

提问by Ducaz035

Today , i have been read all the topic about this but couldn't come up with a solution that's why i am opening this topic.

今天,我已经阅读了有关此主题的所有主题,但无法提出解决方案,这就是我打开此主题的原因。

This is my function which creates the view and i am trying to have a onclick function which should directs to other javascript function where i change the textbox value.

这是我创建视图的函数,我正在尝试使用一个 onclick 函数,该函数应该指向我更改文本框值的其他 javascript 函数。

<script type="text/javascript">
    $('#submitbtnamazon')
            .click(function(evt) {
                        var x = document.getElementById("term").value;

                        if (x == null || x == "" || x == "Enter Search Term") {
                            alert("Please, Enter The Search Term");
                            return false;
                        }
                        listItems = $('#trackList').find('ul').remove();
                        var searchTerm = $("#term").val();
                        var url = "clientid=Shazam&field-keywords="
                                + searchTerm
                                + "&type=TRACK&pagenumber=1&ie=UTF8";       

    jsRoutes.controllers.AmazonSearchController.amazonSearch(url)
                        .ajax({
                            success : function(xml) {
                                $('#trackList')
                                    .append('<ul data-role="listview"></ul>');
                                    listItems = $('#trackList').find('ul');
                                    html = ''

                                 tracks = xml.getElementsByTagName("track");
                                for(var i = 0; i < tracks.length; i++) {
                                    var track = tracks[i];
                                    var titles = track.getElementsByTagName("title");
                                    var artists = track.getElementsByTagName("creator");
                                    var albums =    track.getElementsByTagName("album");
                                    var images =    track.getElementsByTagName("image");
                                    var metaNodes = track.getElementsByTagName("meta");
                                     //trackId ="not found";

                                     trackIds = [];
                                                    for (var x = 0; x < metaNodes.length; x++) {
                                                        var name = metaNodes[x]
                                                                .getAttribute("rel");

                                                        if (name == "http://www.amazon.com/dmusic/ASIN") {
                                                            trackId = metaNodes[x].textContent;
                                                            trackIds.push(trackId);

                                                        }
                                                    }

                                                    for (var j = 0; j < titles.length; j++) {
                                                        var trackId=trackIds[j];
                                                        html += '<div class="span3">'
                                                        html += '<img src="' + images[j].childNodes[0].nodeValue + '"/>';
                                                        html += '<h6><a href="#" onclick="someFunction('
                                                            +trackId
                                                            + ')">'
                                                            +trackId
                                                            + '</a></h6>';
                                                        html += '<p><Strong>From Album:</strong>'
                                                                + albums[j].childNodes[0].nodeValue
                                                                + '</p>';
                                                        html += '<p><Strong>Artist Name:</strong>'
                                                                + artists[j].childNodes[0].nodeValue
                                                                + '</p>';
                                                        html += '<p><Strong>Title:</strong>'
                                                                + titles[j].childNodes[0].nodeValue
                                                                + '</p>';
                                                        /*html += '<p><Strong>Created:</strong>'
                                                                + releaseDate
                                                                + '</p>';*/
                                                        html += '</div>'
                                                    }
                                                }
                                                //listItems.append( html );
                                                $("#track").html(html);
                                                $("#track").dialog({
                                                    height : 'auto',
                                                    width : 'auto',
                                                    title : "Search Results"
                                                });
                                                // Need to refresh list after AJAX call
                                                $('#trackList ul').listview(
                                                        "refresh");
                                            }
                                        });
                    });
</script>

This is my other function where i change the textbox value. it works actually with other values e.g. when i give hardcoded string value. I can see the value in the console but for some reason it gives me the error like : here the string starts with B is AsinId where i take from amazon. I am definitely in need of help because i am totally stucked.

这是我更改文本框值的另一个功能。它实际上适用于其他值,例如当我给出硬编码字符串值时。我可以在控制台中看到该值,但由于某种原因,它给了我这样的错误:这里以 B 开头的字符串是 AsinId,我从亚马逊获取。我绝对需要帮助,因为我完全被困住了。

Uncaught ReferenceError: B00BMQRILU is not defined 62594001:1 onclick

Uncaught ReferenceError: B00BMQRILU is not defined 62594001:1 onclick

<script type="text/javascript">

    function someFunction(var1) {
        tracktextbox = document.getElementsByName("trackId");
        for (var i = 0; i < tracktextbox.length; i++) {
            tracktextbox[i].value = var1;
        }
        $('#track').dialog('close');
    }

</script>

回答by Arun P Johny

The problem is '<h6><a href="#" onclick="someFunction('+trackId+ ')">', from the error it is clear that trackIdis a string value, so you need to enclose it within ""or ''. So try

问题是'<h6><a href="#" onclick="someFunction('+trackId+ ')">',从错误中可以看出这trackId是一个字符串值,因此您需要将其括在""or 中''。所以试试

'<h6><a href="#" onclick="someFunction(\'' + trackId + '\')">'