尝试通过jQuery从输入框中获取值时获取未定义

时间:2013-12-02 19:05:29

标签: javascript jquery html

HTML:

<input value='Rename' type='button'  onclick='RenameGlobalPhase({$row['id']});'
<span id='renameGlobalPhase{$row['id']}'>" . $row['phase'] . "</span>

这是我的JS代码:

function RenameGlobalPhase(id)// {{{
{
    var phase = $('#renameGlobalPhase' + id).html();
    $('#renameGlobalPhase' + id).replaceWith("<input id='#renameGlobalPhase" + id + "' type='text' onblur='SaveNewPhaseName(" + id + ");' value='" + phase + "' />");
    $('#renameGlobalPhase' + id).focus();
} // }}}

function SaveNewPhaseName(id)// {{{
{
    var newPhase = $('#renameGlobalPhase' + id).val();  
    alert(newPhase);
    $('#renameGlobalPhase' + id).replaceWith('<span>' + newPhase + '</span>');
} // }}}

因此,当用户点击上面的input按钮时,我会将其旁边的span转换为input字段,以便用户重命名该值。在onblur(从jQuery为新的input字段新创建)中,我想保存新值并返回span

警报显示undefined值。任何人都可以看到有什么问题吗?

1 个答案:

答案 0 :(得分:4)

#

中删除id

更改

.replaceWith("<input id='#renameGlobalPhase" + id + "'

.replaceWith("<input id='renameGlobalPhase" + id + "'

fiddle