안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2018년 3월 7일 수요일

[javascript] unexpected String

출처: https://stackoverflow.com/questions/22999120/uncaught-syntaxerror-unexpected-string-in-my-javascript

string을 잘못 사용했을때 발생한다.

var options = $(this).data('options').filter('[value="' + checkval + '"]');
와 같이 + 를 사용해서 변수와 string을 이어주자.

I'm getting the Uncaught SyntaxError: Unexpected string error in my JavaScript and I honestly can't figure out what's wrong with the code. I have looked at the similar questions, but I'm unable to find a solution. The error is coming in the line highlighted with an asterisk below.
$("#items1").change(function () {
    if ($(this).data('options') === undefined) {
        $(this).data('options', $('#items2 option').clone());
    }
    var checkval = $(this).val();
/* this line: */ var options = $(this).data('options').filter('[value='"+ checkval +"']');
    $('#items2').html(options);
});
I've added the extra quotes around the checkval to get rid of another error, this might be the problem, but if I change it, the other error returns.

4 Answers

It should be:
var options = $(this).data('options').filter('[value="' + checkval + '"]');
The double quotes need to be inside the single quotes.