按鈕 - 調用函數以確定在文本框的內容中是否找到文本框中的字符串 。當然, 消息給用戶,如果字符串被找到或沒有。從textarea中的文本框搜索字符串
這些是給出的指示,我不知道如何做到這一點。以下是我想出了,我不斷收到此控制檯錯誤:
TypeError: document.getElementById(...) is null file:///........program02javascript/program02script.js Line 10
線10:VAR SEARCHTERM =的document.getElementById( 「text_box_1」)值;
這裏是我的HTML:
<div id="requirement #2">
<h1>Requirement #2</h1>
<form>
Search For:
<input type="text" name="text_box_1">
<br>
</form>
<textarea id="text_area_3"></textarea>
<button type="button" id="button2" onclick="StringSearch()">Search</button>
</div>
這裏是我的javascript:
function StringSearch() {
var SearchTerm = document.getElementById("text_box_1").value;
var TextSearch = document.getElementById("text_area_3").value;
if (TextSearch.match(SearchTerm) === "") {
alert("String Found. Search Complete");
} else {
alert("No Data found in Text Area");
}
}
我怎樣才能完成這個任務?我不知道我完全明白該怎麼做。我試圖找到一些示例代碼,但沒有遇到任何有用的東西。
嗯好的,非常感謝!如果我想展示發現它的句子或以某種方式顯示它被發現的地方......我將如何去做這件事?是簡單地添加更多的代碼行,還是需要以完全不同的方式進行操作? – kronis72
@ kronis72當你使用'indexOf()',你會得到找到匹配的單詞的索引,你可以使用這個索引來提取句子/單詞 – Tushar