[英]javascript, HTML Form: Click button to insert NULL
這是我正在使用的代碼,可以在textarea字段中運行,但不能在輸入字段中運行。 如何修改代碼以便可以在輸入字段中運行?
<head> <script type="text/javascript"> function insertText(elemID, text) { var elem = document.getElementById(elemID); elem.innerHTML = text; } </script> </head> <form> <textarea id="txt1" name="passport_no" rows="1" style="width: 100px" required="required"></textarea> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt1', 'NULL');"> <br><br> <textarea id="txt2" name="hkid_no" rows="1" style="width: 100px" required="required"></textarea> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt2', 'NULL');"> </form>
我試圖將其修改為如下所示的輸入字段,但無法運行。
<form> <input id="txt1" name="passport_no" style="width: 100px" required="required"> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt1', 'NULL');"> <br><br> <input id="txt2" name="hkid_no" style="width: 100px" required="required"> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt2', 'NULL');"> </form>
將.innerHTML
替換為.value
以設置<input>
元素的value
屬性
值
控件的初始值。
<script type="text/javascript"> function insertText(elemID, text) { var elem = document.getElementById(elemID); elem.value = text; } </script> <form> <input id="txt1" name="passport_no" style="width: 100px" required="required"> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt1', 'NULL');"> <br> <br> <input id="txt2" name="hkid_no" style="width: 100px" required="required"> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt2', 'NULL');"> </form>
通過設置elem
的value
來嘗試:
<html>
<head>
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML = text;
elem.value = text;
}
</script>
</head>
<form>
<textarea id="txt1" name="passport_no" rows="1" style="width: 100px" required="required"></textarea>
<input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt1', 'NULL');">
<br><br>
<textarea id="txt2" name="hkid_no" rows="1" style="width: 100px" required="required"></textarea>
<input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt2', 'NULL');">
</form>
</html>
首先,您需要檢查輸入標簽還是文本區域。 如果是textarea,則可以使用innerHTML函數,但如果是Input標記,則需要使用.value函數。
編碼愉快!
z
function insertText(elemID, text) { var elem = document.getElementById(elemID); console.log(elem.getAttribute("type")); // check elem is input or Text area if (elem.tagName == "INPUT" && elem.getAttribute("type") == "text") { elem.value = text; } if (elem.tagName == "TEXTAREA") { elem.innerHTML = text; } }
<form> <input type="text" id="txt1" /> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt1', 'NULL');"> <br><br> <textarea id="txt2" name="hkid_no" rows="2" col="3" style="width: 100px" required="required"></textarea> <input type="button" value="If no value, click here to input NULL !" onclick="insertText('txt2', 'NULL');"> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.