1

我有一个显示用户名的简单 DIV:

<div id="firstNameChange" title="Click to edit" style="vertical-align:top; display:inline-block; float:left;"><?=$firstName?></div>

当用户用鼠标单击 div 时,它会变成一个表单:

<form method="post" action="" >
  <div id="firstNameChange" title="Click to edit" style="vertical-align:top; display:inline-block; float:left;"><?=$firstName?></div>
  <input type="hidden" name="firstName" id="firstName" value="<?=$firstName?>"/>
  <input type="submit" name="submitFirstName" id="saveFirstName" class="ui-icon ui-icon-disk" style="border:none; display:none; background-color:transparent; float:right; vertical-align:top;" />
</form>

使用 jQuery:

$("#firstNameChange").click(function() { 
    //check if there are any existing input elements
    if ($(this).children('input').length == 0) {
        $("#saveFirstName").css("display", "inline");
        //variable that contains input HTML to replace
        var inputbox = "<input type='text' class='inputbox' name='firstName' value=\""+$(this).text()+"\">";    
        //insert the HTML intp the div
        $(this).html(inputbox);
        //automatically give focus to the input box     
        $(".inputbox").focus();
        //maintain the input when it changes back to a div
        $(".inputbox").blur(function() {
            var value = $(this).val();
            $("#firstName").val(value);
            $("#firstNameChange").text(value);
        });
    }               
});

问题是表单仅在用鼠标单击提交按钮时才发回服务器。如果用户要按键盘上的回车键,它不会回发。有什么建议么?

4

1 回答 1

0

找到了答案。我删除了隐藏元素,一切正常。感谢大家的支持!

于 2013-01-16T12:42:25.990 回答