我使用Bootstrap,我有兩個相同的形式。我正在嘗試向Google搜索結果添加表單提交,並且它可以正常工作,但是當我包含兩個相同的表單時,由於這兩個表單的ID相同,因此無法工作。我怎樣才能解決這個問題?該ID需要相同,因爲谷歌尋找「G」。我有兩種形式的原因是因爲我在移動設備上的顯示方式不同。使用媒體查詢。以下是我的代碼謝謝。問題與表單驗證Javascript
<form name="globalSearch" class="navbar-form" role="search" form action="" onsubmit="return validateSearch()">
<div class="input-group add-on">
<input type="hidden" name="cx" value="" />
<input type="hidden" name="cof" value="FORID:11;NB:1" />
<input type="hidden" name="ie" value="UTF-8" />
<input class="form-control" placeholder="Search entire site..." id="q" name="q" type="text">
<div class="input-group-btn">
<button class="btn btn-default btnSubmit" type="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
function validateSearch() {
if (globalSearch.q.value.length == 0) {
document.getElementById("q").value = "Enter a Value";
document.getElementById("q").style.color = "red";
return false;
}
}
在單個頁面上重複ID是個壞習慣。使用不同的ID並保持相同的名稱。 –
「q」對於Google來說是必需的。我不能改變它 – Tom
你確定這是必要的ID,而不只是在名稱屬性?正如我可以從google.com搜索頁面看到的,他們只是使用name屬性和自定義ID。 –