2011-12-29 29 views
0

我是javascript新手。这里是我的代码基本的JavaScript事件

<script> 
function text_input_type(type) 
{ 
if(type=='list'){ 
document.getElementById("note_input").innerHTML="<input type=\"text\" name=\"body\">"; 
} 
else{ 
document.getElementById("note_input").innerHTML="<textarea id=\"note_input\" name=\"body\" cols=\"27\" rows=\"5\"></textarea>"; 
} 
} 

</script> 

<textarea id="note_input" name="body" cols="27" rows="5"></textarea> 
<input type="radio" name="type" value="text" onclick=text_input_type('list') /> 
<input type="radio" name="type" value="list" onclick=text_input_type('text') /> 

我想它,以便取决于哪个单选按钮,按下它从一个textarea的输入文本类型的变化。问题不在于将输入从文本框更改为较小的文本输入,而是将框中的代码打印出来。

+0

您面临的问题是什么? – AmGates 2011-12-29 08:40:42

+0

事件不得发射。错过了引号,同时将功能分配给DOM中的事件 – Anand 2011-12-29 08:44:23

+0

对不起,我忘了一部分。我添加了它。 – 2011-12-29 08:45:57

回答

1

希望这有助于您解决问题。

<script> 
    function text_input_type(type) 
    { 
    if(type=='list'){ 
    document.getElementById("note_input").innerHTML="<input type=\"text\" id=\"note_input1\" name=\"body\">"; 
    } 
    else{ 
    document.getElementById("note_input").innerHTML="<textarea id=\"note_input1\" name=\"body\" cols=\"27\" rows=\"5\"></textarea>"; 
    } 
    } 

    </script> 

    <div id="note_input"><textarea id="note_input1" name="body" cols="27" rows="5"></textarea></div> 
    <input type="radio" name="type" value="text" onclick=text_input_type('list') /> 
    <input type="radio" name="type" value="list" onclick=text_input_type('text') /> 

试试这个代码,你会得到你想要的东西。

0

你的代码是正确的,除了一个小的改变。

<html> 
<body> 
<input type="radio" name="type" value="text" onclick="text_input_type('list');" /> 
<input type="radio" name="type" value="list" onclick="text_input_type('text');" /> 
<div id="note_input"> 
</body> 
</html> 

应该正常工作

0

绑定单击处理用了Javascript,—联JavaScript是不是真的有必要:

var elems = [].slice.call(document.getElementsByTagName("input")); 
elems.forEach(function(elem){ 
    elem.onclick = function(){ 
     var type = this.value; 
     if(type === 'list'){ 
      alert("type is list"); 
     } else { 
      alert("type is not list"); 
     } 
    }; 
}); 

Example

我知道这可能有点复杂。我们只是在页面上的每个输入标签上附加一个点击功能。我们将点击输入的value设置为type变量,并检查该变量是否等于字符串list。如果是,那么我们在if中激发代码。如果不是,我们在else中激发代码。

基本上这样做是为了让你更容易。你只需将这段代码放到你的JS文件中,你不必担心在元素本身上分配onclick(而且看起来你正在为它们中的两个做它)。

然而,如果你的周围有onclick报价您的代码将正常工作,就像这样:

onclick="text_input_type('list');"