我試圖在選擇單選按鈕時更新HTML5佔位符屬性。我沒有使用JQuery,所以更喜歡內聯JavaScript解決方案。我知道我錯過了一些簡單的事情,但我正在努力教導自己!當選擇無線電輸入時修改佔位符屬性
<script type="text/javascript">
function ModifyPlaceHolder1() {
var input = document.getElementById ("MyQuery");
input.placeholder = "Search books e.g. Harry Potter";
}
function ModifyPlaceHolder2() {
var input = document.getElementById ("MyQuery");
input.placeholder = "Search journals e.g. New Scientist";
}
</script>
<input type="text" id="MyQuery" placeholder="Search resources" name="q" />
<input type="radio" value="" id="All" name="s.cmd" checked="checked" />
<label for="All">All</label>
<input type="radio" onclick="ModifyPlaceHolder1()" value="" id="Books" name="s.cmd" checked="checked" />
<label for="Books">Books</label>
<input type="radio" onclick="ModifyPlaceHolder2()" value="" id="Journals" name="s.cmd" checked="checked" />
<label for="Journals">Journals</label>
似乎是工作的罰款..? http://jsfiddle.net/dhLfx/ – PSL
從技術上講,避免內聯JavaScript會更好。 –