Javascript 按ENTER键时表单未过帐到服务器
我有一个简单的DIV,显示用户的名称:Javascript 按ENTER键时表单未过帐到服务器,javascript,jquery,html,Javascript,Jquery,Html,我有一个简单的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="firs
<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按钮时才会发回服务器。如果用户按键盘上的enter键,它将不会发回。有什么建议吗?找到了答案。我去掉了隐藏的元素,一切都很好。谢谢大家的支持 我在这里设置了它,它在我的浏览器Google Chrome 24.0.1312.52 Mac OS X中工作。您使用的是其他浏览器,还是问题中未包含的某些代码有问题?您能否尝试为action属性输入一些内容,即action='/'。我认为问题在于$.inputbox.Blur函数{如果您在文本字段中按enter键,将不会调用。@SamuelEdwinWard-我正在使用IE 9.0.8,但我没有成功…JFIDLE对我有效,但我的应用程序仍然没有…当我删除隐藏元素时,它工作得很好…完全奇怪!@massimorai,我想这就是您的答案…隐藏元素似乎不必要你应该把它贴出来接受。