当按下ENTER键时,表单不发送到服务器

Form not posting to server when ENTER key is pressed

本文关键字:服务器 表单 ENTER 键时      更新时间:2023-09-26

我有一个显示用户名的简单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);
        });
    }               
});

问题是表单只有在用鼠标单击Submit按钮时才会发送回服务器。如果用户按了键盘上的回车键,它就不会返回。有什么建议吗?

找到答案了。我移除了隐藏元素,一切都运行良好。感谢大家的支持!

相关文章: