最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript Eval() generating 'Unexpected Token ) error' - Stack Overflow

programmeradmin1浏览0评论

I'm having a problem with the following bit of code and I'm wondering what I'm doing wrong.

I'm getting an Uncaught SyntaxError: Unexpected token ) error on the line where the eval call is made.

function myfunction(){
    var p1 = '';
    var p2 = '';
    var p3 = '';

    for (i=1; i<=3; i++){
        eval("$('#p"+i+"').closest('.filter').find('.vals div').each(function(){if ($.trim(p"+i+").length > 0) {p"+i+" += ',';} p"+i+" += $(this).attr('class');});");
    }
}

Here is the applicable HTML:

<div class="filter">
    <label>Organizations</label>
    <input id="p1" type="text" value="" />
    <div class="vals">
        <div class="3" title="Click to remove">ABC School District</div>
        <div class="4" title="Click to remove">DEF School District</div>
    </div>
</div>
<div class="filter">
    <label>Groups</label>
    <input id="p2" type="text" value="" />
    <div id="vals"></div>
</div>

If it isn't pletely obvious I'm also using jQuery here.

Thanks

I'm having a problem with the following bit of code and I'm wondering what I'm doing wrong.

I'm getting an Uncaught SyntaxError: Unexpected token ) error on the line where the eval call is made.

function myfunction(){
    var p1 = '';
    var p2 = '';
    var p3 = '';

    for (i=1; i<=3; i++){
        eval("$('#p"+i+"').closest('.filter').find('.vals div').each(function(){if ($.trim(p"+i+").length > 0) {p"+i+" += ',';} p"+i+" += $(this).attr('class');});");
    }
}

Here is the applicable HTML:

<div class="filter">
    <label>Organizations</label>
    <input id="p1" type="text" value="" />
    <div class="vals">
        <div class="3" title="Click to remove">ABC School District</div>
        <div class="4" title="Click to remove">DEF School District</div>
    </div>
</div>
<div class="filter">
    <label>Groups</label>
    <input id="p2" type="text" value="" />
    <div id="vals"></div>
</div>

If it isn't pletely obvious I'm also using jQuery here.

Thanks

Share Improve this question edited Oct 26, 2011 at 13:36 Ryan asked Oct 26, 2011 at 13:18 RyanRyan 6,86613 gold badges51 silver badges68 bronze badges 2
  • 5 "I'm wondering what I'm doing wrong" -- My answer would be using eval() in the first place ... You have a hard to debug statement. I don't see why you can't write this without eval() ... – Martin Tournoij Commented Oct 26, 2011 at 13:22
  • You can skip using eval for statements like these "$('#p"+i+"')... by simply using $('#p['+i) as jquery takes the selecter as string. I really do not see any advantage of eval here. – Birey Commented Oct 26, 2011 at 13:30
Add a ment  | 

3 Answers 3

Reset to default 5

You are better off using an array and just executing the code normally:

function myfunction(){
    var p  = [null, '', '', '']; //Empty zeroth element to keep your 1-indexing

    for (i=1; i<=3; i++){
        $('#p'+i).closest('.filter').find('.vals div').each(function(){
            if ($.trim(p[i]).length > 0) {
                p[i] += ',';
            }
            p[i] += $(this).attr('class');
        });
    }
}

Your code, no eval

function myfunction(){
    var p1 = '';
    var p2 = '';
    var p3 = '';

    for (i=1; i<=3; i++){
        $("#p"+i).closest(//..... rest here

You might be missing a "

 eval("$('#p" + i + "'")...

FWI: Instead of nesting functions and chaining, you'll find it much easier to break it into several discrete lines of code and use named functions.

发布评论

评论列表(0)

  1. 暂无评论