我有形式,看起來像這樣:輸入文本輸入觸發按鈕點擊
<form>
Place straightedge/yardstick along width of pothole such that it points at the corners, <a class="showImage">like this</a>
<span class="row">
<label class="firstColumn seventeenTwentieths">Pothole width (in inches): </label>
<input type="text" class="secondColumn tenth numberField" id="potholeWidth" />
</span>
<span class="rowTight">
<label class="firstColumn seventeenTwentieths">Interval size (in inches, default 1 inch): </label>
<input type="text" class="secondColumn tenth numberField" id="intervalSize" value="1" />
</span>
<div class="rowTight">
<label class="firstColumn">Do any of the edges intersect the straightedge/yardstick other than at the corners?</label>
<div class="secondColumn">
<span>
<input type="radio" name="concaveEdges" id="yesConcaveEdges" />
<label for="yesConcaveEdges">Yes</label>
</span>
<br>
<span>
<input type="radio" name="concaveEdges" id="noConcaveEdges" checked />
<label for="noConcaveEdges">No</label>
</span>
</div>
</div>
<span class="rowTight">
<label class="firstColumn half">Spreadsheet name: </label>
<input type="text" class="secondColumn nineTwentieths" id="spreadsheetName"/>
</span>
<span class="center row">
<button class="center" id="clearForm">Clear</button>
<input type="submit" class="center action" value="Create spreadsheet" />
</span>
</form>
,由於某種原因,甚至當我阻止上的click事件的默認操作中的任何<input type="text">
領域,它仍然觸發.click()
對於唯一的<button>
,這是而不是用於提交表單的時候,按回車。 (我想確認按鈕提交表單。)
我的事件偵聽器的代碼是這樣的:
$(document).ready(function(){
$('a.showImage').click(showImage);
$('#imageContainer .close').click(hideImage);
$('#clearForm').click(function(event) {
clearForm();
event.preventDefault();
});
// prevent default action when user clicks a button
/*$('input[type="submit"], button').each(function(index) {
$(this).click(function(event) {
//if ($(this) == $('input[type="submit"]'))
if (index == 0)
submitFormData();
event.preventDefault();
});
});*/
$('input[type="submit"]').click(function(event) {
event.preventDefault();
submitFormData();
});
$('#emailLink').click(emailLink);
$('input[type="text"]').each(function() {
var that = this;
$(this).keyup(function(event) {
// allow for 'submit' on enter button for any field
if (event.key == 'Enter')
{
event.preventDefault();
submitFormData();
}
// if this is a numberField, validate it like one
else if ($(that).hasClass('numberField'))
{
// blacklisting again!!
if ((!event.key.match(/[,.\d]/)) && (event.key != 'Tab'))
{
if (!$(that).hasClass('invalid')) $(that).addClass('invalid');
}
// if user 'Backspace'd or 'Delete'd on something
if ((event.key == 'Backspace') || (event.key == 'Delete'))
{
// make sure that only the allowed characters are in the field
if ($(that).val().match(/^\d{1,3}\.?\d*$/))
{
if ($(that).hasClass('invalid')) $(that).removeClass('invalid');
}
}
}
});
});
})
什麼,如果有的話,我會在事件消耗做錯了什麼? 如果需要,可以提供完整網站here。
您鏈接的網站上沒有錯誤/問題。當我在文本框中輸入文本時,它們運行良好。沒有按鈕按下事件發生。 – ash
我更新了問題。問題是在任何文本字段中按ENTER鍵。 –
你爲什麼使用每個循環? $('input [type =「text」]')。keyup會綽綽有餘嗎?該功能將針對頁面上具有文本類型的任何輸入字段執行。然後你可以在那裏運行你的邏輯。 –