2010-12-06 84 views
0

使用javascript,我希望我的复选框更改输入文本字段的文本修饰。 所以当它被选中时,输入文本字段中的文本变成粗体。复选框更改文本装饰输入字段

记住我学习,所以我不亲,你的家伙;)

我想是这样的;

var checkbox = create("input"); 
    checkbox.type = "checkbox"; 
    checkbox.id = "checkboxId" + counter; 
    div.appendChild(checkbox); 
    checkbox.onClick="boldChange(this)" 

var input = create("input"); 
    input.type = "input"; 
    input.id = "inputId" + counter; 
    div.appendChild(input); 



function boldChange() 
var boldgroup = document.getElementsByName(el.name); 
for (var b=0; boldgroup[b]; ++b) 
inputId.style.textDecoration = boldgroup[b].checked ? 'bold' : 'none'; 
} 

我该如何做这项工作? 非常感谢你提前

+0

` text-decoration:bold`是无效的CSS,IIRC – 2010-12-06 17:54:11

回答

2

这是基于你上面的代码工作的jsfiddle例如:Link to example

代码片段:(地方下面</body>,使所有的DOM加载)

<script> 
    var div = document.getElementById('div'), 
    counter = 0; 

    var checkbox = document.createElement("input"); 
    checkbox.type = "checkbox"; 
    checkbox.id = "checkboxId" + counter; 
    div.appendChild(checkbox); 
    checkbox.onclick = boldChange; 
    counter++; 

    var input = document.createElement("input"); 
    input.type = "text"; 
    input.id = "inputId" + counter; 
    div.appendChild(input); 



    function boldChange() { 
     input.style.fontWeight = (checkbox.checked)?'bold':'normal'; 
    } 
</script> 
+0

非常感谢你!完美作品 – Opoe 2010-12-06 18:23:35