<!DOCTYPE html>
<html>
<body>
<textarea id="iamtextarea" rows="4" cols="10" onfocus="onFocusTextArea();" onblur="onBlurTextArea();"></textarea>
<input type="checkbox" name="iamcheckbox" id="iamcheckbox" checked="checked"> I am checkbox<br>
</body>
</html>
<script type="text/javascript">
function onFocusTextArea(){
document.getElementById("iamcheckbox").checked = false;
}
function onBlurTextArea(){
if(document.getElementById("iamtextarea").value==""){
document.getElementById("iamcheckbox").checked = true;
}
}
</script>
運行上面的代碼它會做所需的工作!
Iinjoy!
現在你必須添加一個隱藏字段,並在onBlurTextArea功能有一定的變化,看下面的代碼:
<html>
<body>
<textarea id="iamtextarea" rows="4" cols="10" onfocus="onFocusTextArea();" onblur="onBlurTextArea();">Enter some text in textbox</textarea>
<input type="checkbox" name="iamcheckbox" id="iamcheckbox" checked="checked"> I am checkbox<br>
<input type="hidden" name="hiddenString" id="hiddenString" value="Enter some text in textbox">
</body>
</html>
<script type="text/javascript">
function onFocusTextArea(){
document.getElementById("iamcheckbox").checked = false;
}
function onBlurTextArea(){
if(document.getElementById("iamtextarea").value==document.getElementById("hiddenString").value){
document.getElementById("iamcheckbox").checked = true;
}
}
</script>
是否有可能增加對這樣的: IF「iamtextarea」爲空字符串或默認字符串 到上面的代碼? –
告訴我dafault字符串...我將編輯答案 – Anubhav
默認字符串來自數據庫條目並作爲php變量回顯:<?= $ iamdefaultstring?>。默認字符串可以另一種形式編輯,以便一組用戶可以編輯默認文本應該是什麼,另一組用戶可以選擇使用默認文本或用自己的默認文本覆蓋它。 –