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

时间:2013-12-27 14:41:34

标签: javascript jquery

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

这是我创建视图的函数,我正在尝试使用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>

这是我改变文本框值的另一个功能。它实际上与其他值有关,例如当我给硬编码的字符串值。我可以在控制台中看到该值但由于某种原因它给出了如下错误: 这里的字符串以B开头是AsinId,我从亚马逊那里拿走。我肯定需要帮助,因为我完全陷入困境。

未捕获的ReferenceError:未定义B00BMQRILU 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>

1 个答案:

答案 0 :(得分:12)

问题是'<h6><a href="#" onclick="someFunction('+trackId+ ')">',错误很明显trackId是字符串值,因此您需要将其括在""''中。所以试试

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