2010-09-11 73 views
0

我試圖用一個文本字段替換下拉菜單,如果有人選擇「其他」選項。現在在我的代碼中,我用它代替了一個段​​落元素,因爲我懶得找到設置文本字段的確切構造函數參數。但是,選擇「其他」時不會發生任何事情。用javascript中的文本菜單替換下拉菜單

有沒有人知道這有什麼問題?

<html> 
<head> 
<script type="text/javascript"> 
function testfunc(arg) { 
    if(arg.value == "other") { 
     document.thing.replaceChild(document.test, document.thing.selection) 
    } 
    else { 
     alert("stuff") 
    } 
} 
</script> 
<body> 
<form name="thing"> 
<select name="selection" onchange="testfunc(document.thing.selection.options[document.thing.selection.selectedIndex])"> 
<option>yes</option> 
<option>no</option> 
<option>other</option> 
</select> 
</form> 
<p name="test">lkjsdf</p> 
</body> 
</html> 
+0

這是僞代碼嗎? – Lazarus 2010-09-11 07:34:50

回答

0
function show_txt(arg,arg1) 
{ 
if(document.getElementById(arg).value=='other') 
{ 
document.getElementById(arg1).style.display="block"; 
document.getElementById(arg).style.display="none"; 
} 
else 
{ 
document.getElementById(arg).style.display="block"; 
document.getElementById(arg1).style.display="none"; 
} 
} 

的HTML代碼在這裏:

<select id="arg" onChange="show_txt('arg','arg1');"> 
<option>yes</option> 
<option>No</option> 
<option>Other</option> 
</select> 
<input type="text" id="arg1" style="display:none;"> 
2

功能應該是這樣的!其他不是其他!其大小寫敏感(在Linux反正...不知道其他的操作系統),否則謝謝...一直在尋找這個...秀酷路/隱藏

<script type="text/javascript"> 
function show_txt(arg,arg1) 
{ 
if(document.getElementById(arg).value=='Other') 
{ 
document.getElementById(arg1).style.display="block"; 
document.getElementById(arg).style.display="none"; 
} 
else 
{ 
document.getElementById(arg).style.display="block"; 
document.getElementById(arg1).style.display="none"; 
} 
} 
</script> 
0

嘿,我看到了你的代碼,你需要一些練習,

你忘了關閉tagh。其次總是使用ID而不是名稱。第三種形式不能訪問<p> tagh。第四,您無法使用表單前綴訪問其他表單元素。第五要麼你可以創建<p> tagh dynamicaly或在頁面上顯示none。檢查我的代碼。這也是你可以做的動態生成它。

var newP = document.createElement(「p」); var txt ='lkjsdf'; var newT = document.createTextNode(txt); newP.appendChild(newT);

<html> 
<head></head> 
<script type="text/javascript"> 
function testfunc(arg) { 
    if(arg.value == "other") { 
     document.thing.removeChild(document.thing.selection); 
     document.getElementById('testText').style.display='inline'; 
    } 
    else { 
     alert("stuff"); 
    } 
} 
</script> 
<body> 
<form name="thing"> 
<select name="selection" onchange="testfunc(document.thing.selection.options[document.thing.selection.selectedIndex])"> 
<option>yes</option> 
<option>no</option> 
<option>other</option> 
</select> 
<p id="testText" style="display:none">lkjsdf</p> 
</form> 
</body> 
</html>