jQuery insertAfter()在空间上出现故障

时间:2013-10-28 18:42:14

标签: jquery

var lastname = jQuery('[name="dc_contributor_author_first_'+i+'"]');
if (lastname==null)
    exit;
var orderbox = "<td nowrap='nowrap'><input type='text' size=1 id='author_order_"+i+"' value="+i+" onchange=updateBoxes(this.value,"+i+")></td>";
jQuery(orderbox).insertAfter(lastname.parent());

结果为

<input type="text" )="" 1="" onchange="updateBoxes(this.value," value="1" id="author_order_1" size="1">

但是当我从updateBoxes中删除空格作为"onchange=updateBoxes(this.value,"+i+")"函数时,它的工作正常,结果如下。为什么呢?

<input type="text" onchange="updateBoxes(this.value,1)" value="1" id="author_order_1" size="1">

2 个答案:

答案 0 :(得分:1)

尝试更改:

var orderbox = "<td nowrap='nowrap'><input type='text' 
size=1 id='author_order_"+i+"' value="+i+" 
onchange=updateBoxes(this.value,"+i+")></td>";

要:

var orderbox = "<td nowrap='nowrap'><input type='text' 
size='1' id='author_order_"+i+"' value='"+i+"' 
onchange='updateBoxes(this.value,"+i+")'></td>";

对于jQuery解析器,正确的属性引用可能更友好。

答案 1 :(得分:0)

不确定lastname = if(lastname==null) exit;的含义,但是您应该如何创建和插入这些元素:

var td  = $('<td />', {
        'nowrap':'nowrap'
    }),
    inp = $('<input />', {
        type  : 'text',
        size  : '1',
        id    : 'author_order_'+i,
        value : i,
        on    : {
                change: function() {
                    updateBoxes(this.value, i);
                }
        }
    });

$('[name="dc_contributor_author_first_'+i+'"]').parent().after(td.append(input));
相关问题