gpt4 book ai didi

javascript - 按 enter 时表单未提交

转载 作者:技术小花猫 更新时间:2023-10-29 11:46:55 26 4
gpt4 key购买 nike

我有以下 HTML/JS/jQuery 代码。此代码代表一个登录表单,该表单以模态方式呈现给用户以允许他们登录。问题是,当我按下回车键时,表单似乎没有执行“onsubmit”事件。当我单击表单底部的按钮时(它与 onsubmit 事件的代码几乎相同),它工作得很好。我想知道是否有人可以告诉我为什么这个表格没有提交..?如有任何帮助,我们将不胜感激。

显示登录模式的 jQuery 代码:

showDivAndFocus('loginModal','loginaccount'); 

function showDivAndFocus(v,t){

if (api)
if (api.isOpened)
api.close();

api = $('#'+v).overlay({
mask: {color: '#000000'},
top:'0px',
api: true,
autoScrollToActive: false,
autoScrollOffset: 0
}).load();

document.getElementById(t).focus();
}

HTML 代码

<div class="modal" id="loginModal">
<h2>User Login</h2>
<br />

<form action="javascript:void(0);" onsubmit="return(doLogin());" name="loginForm" id="loginForm">
<table width="95%" cellpadding="4" cellspacing="4">
<tr>
<td class="regw" align="left"><b>Account Number:</b></td>
<td class="regw" align="left"><input type="text" maxlength="10" size="10" name="loginaccount" id="loginaccount" /></td>
</tr>

<tr>
<td class="regw" align="left"><b>Username:</b></td>
<td class="regw" align="left"><input type="text" maxlength="20" size="20" name="loginusername" id="loginusername" /></td>
</tr>

<tr>
<td class="regw" align="left"><b>Password:</b></td>
<td class="regw" align="left"><input type="password" maxlength="20" size="20" name="loginpassword" id="loginpassword" /></td>
</tr>

<tr>
<td class="regw" align="left"><b>Remember Me:</b></td>
<td class="regw" align="left"><input type="checkbox" name="loginremember" id="loginremember" /></td>
</tr>

<tr><td colspan="2">
<div>
<center>
<table><tr><td width="50" valign="middle">
<div id="loginLoading" style="height:24px;width:24px;"></div>
</td><td>
<button onclick="doLogin();" type="button" class="ok">Submit</button>
</td><td>
<button onclick="api.close();" type="button" class="cancel">Cancel</button>
</td><td width="50">&nbsp;</td></tr></table>
</center>
</div>
</td></tr>
</table>
</form>
</div>

AJAX 调用

function doLogin(){
var ajax = getXmlObject();
var f = getFormVariables();
var url= '/login.php?f=' + encodeURIComponent(f);
if (ajax.readyState == 4 || ajax.readyState == 0) {
ajax.open("POST", url, true);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
var a = ajax.responseText;
if (a=="OK"){...} else {...}
}
};
ajax.send(null);
}
return false;
}

最佳答案

我也在为同样的问题苦苦挣扎;在文本字段中按“输入”时我的一个表单正在提交,没有问题;在我的一生中,同一页面上的另一个类似的表格根本不会提交。

这两个字段都没有提交按钮,也没有使用 javascript 进行任何提交。

我发现,当表单中只有一个文本字段时,在文本字段中按“enter”将自动提交;但如果有多个(常规(即单行)文本输入)字段,它什么也不做,除非还有某种“提交”按钮。

显然这是 HTML 2.0 specification 的一部分:

When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.

An old, but apparently still valid, and interesting, further discussion here .

... evidently meant as a convenient way to submit simple queries, but reducing the risk, on a complex form, of prematurely submitting it while trying to fill it in. Numerous browser implementers (e.g Netscape, Opera, various versions of Lynx,...) followed this advice, though it seems with slightly different interpretations.

我做了 a JSFiddle to demonstrate .据我所知(懒惰地只是用 Chrome 测试),如果只有一个文本字段,或者如果有一个提交按钮,即使它是隐藏的,表单也会在“输入”时提交。

(编辑:我后来发现,如果有其他不是常规的单行文本输入的输入字段,它似乎也能工作……例如文本区域、选择等——感谢@user3292788该信息。更新 JSFiddle 以反射(reflect)这一点)。

<h2>Form with one text field only, no submit button</h2>
<p>Seems to submit automatically when pressing 'enter' in the first text field</p>
<form action="" method="post">
<div>
<label for="pt-search-input">Search</label>
<div>
<input name="term" type="text" placeholder="Example: budapest amsterdam" /> <a href="#pt-search-bar">cancel</a></div>
</div>
</form>

<h2>Form with two text fields, no submit button</h2>
<p>Won't submit when pressing 'enter' in the forms ...</p>
<form action="" method="post">
<div>
<label for="pt-search-input">Search</label>
<div>
<input name="term" type="text" placeholder="Example: budapest amsterdam" />
<input name="term2" type="text" placeholder="Example: budapest amsterdam" /> <a href="#pt-search-bar">cancel</a></div>
</div>
</form>



<h2>Form with two text fields and a submit button ...</h2>
<p>Now it submits with 'enter' key</p>
<form action="" method="post">
<div>
<label for="pt-search-input">Search</label>
<div>
<input name="term" type="text" placeholder="Example: budapest amsterdam" />
<input name="term2" type="text" placeholder="Example: budapest amsterdam" /> <a href="#pt-search-bar">cancel</a>
<input type="submit" />
</div>
</div>
</form>

<h2>Form with two text fields and a HIDDEN submit button ...</h2>
<p>Seems to work, even with submit hidden ...</p>
<form action="" method="post">
<div>
<label for="pt-search-input">Search</label>
<div>
<input name="term" type="text" placeholder="Example: budapest amsterdam" />
<input name="term2" type="text" placeholder="Example: budapest amsterdam" /> <a href="#pt-search-bar">cancel</a>
<input type="submit" />
</div>
</div>
</form>


<h2>Form with no action or method attribute, HIDDEN submit button ...</h2>
<p>Even this seems to work.</p>
<form>
<div>
<label for="search-input">Search</label>
<div>
<input name="term" type="text" placeholder="Example: budapest amsterdam" />
<input name="term2" type="text" placeholder="Example: budapest amsterdam" /> <a href="#pt-search-bar">cancel</a>
<input type="submit" />
</div>
</div>
</form>

<h2>Form with multiple fields, but only one text input, no submit button.</h2>
<p>This seems to work as well.</p>
<form action="" method="post">

<p><input type="text" ></p>

<textarea name="" id="" cols="30" rows="10"></textarea>

<p>
<select name="" id="">
<option value="">Value</option>
</select>
</p>
</form>

关于javascript - 按 enter 时表单未提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4196681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com