2017-02-08 7 views
0

この質問のバリエーションが他のスレッドにあることは知っていますが、私の答えは私には役に立たないようです。うまくいけばそれはかなり単純なものです...私は間違って何をしていますか?Javascriptのstyle.displayがフォームと連携していない

私はオプションフィールドを持っています。ユーザーがドロップダウンオプションとして「Between」を選択すると、別の入力ボックス(この場合は「AdditionalThreshold」)を追加します。

function toggleMe(a) { 
 
    var e=document.getElementByName("AdditionalThreshold"); 
 
    if(e.style.display=="none"){ 
 
    e.style.display = "block"; 
 
    } else { 
 
    e.style.display = "none"; 
 
    } 
 
    return true; 
 
}
<td> 
 
    <select name="ThresholdType"> 
 
    <option value="GreaterThan">Greater than or Equal to</option> 
 
    <option value="Between" onClick="toggleMe('BetweenField')">Between</option> 
 
    <option value="LessThan">Less than</option> 
 
    </select> 
 
</td> 
 
<td> 
 
    <input name="Threshold" type="text" size="4" /> 
 
    <input name="AdditionalThreshold" type="text" id="BetweenField" size="4" style="display:none;"> 
 
</td>

私は私のコーディングを許し、この時にはかなりの初心者ですが、任意の助けもいただければ幸いです。

おかげで

+0

を(あなたが欲しい1 thatsのと仮定して)これは、あなたがそれを望むように動作しませんか? – qxz

+0

ドロップダウンから 'Between'オプションが選択されているときに表示され、別のオプションが選択されているときは非表示にする非表示(デフォルトでは 'display:none')ボックスがあります。 –

+1

@ObsidianAgeうん、わかった。 OPは彼の現在のコードがいかに働いていないか言っていない。 – qxz

答えて

0

document.getElementById('ThresholdType').addEventListener("change", function(){ 
 
\t var visibility = (this.value === 'Between')?"block":"none"; 
 
    console.log(this.value); 
 
    document.getElementsByName('AdditionalThreshold')[0].style.display = visibility; 
 
})
<select id="ThresholdType"> 
 
     <option value="GreaterThan">Greater than or Equal to</option> 
 
     <option value="Between">Between</option> 
 
     <option value="LessThan">Less than</option> 
 
    </select> 
 
    <input id="Threshold" type="text" size="4" /> 
 
    <input name="AdditionalThreshold" type="text" id="BetweenField" size="4" style="display:none;">

+0

ありがとう@scraaappy。これは素晴らしく、まさに私が何をしているかです。皆さんのご支援に感謝します。 – Tom

2

私はあなたが使用することを意味だと思う:

document.getElementsByName("AdditionalThreshold") 

その場合には、アレイ状の構造体がのNodeListと呼ば返します。 だから、最初のものを選択するように

document.getElementsByName("AdditionalThreshold")[0]; 

をしたいと思います。どのようにして、

+0

技術的に['getElementsByName'](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName)は配列を返しませんが、ライブ[' NodeList'コレクション](https ://developer.mozilla.org/en-US/docs/Web/API/NodeList) – qxz

+0

@qxz正しいはずです。それを私が直した。その配列のようなものですが、通常の配列とは異なります。 – Ju66ernaut

0
<select name="ThresholdType"> 
    <option value="GreaterThan">Greater than or Equal to</option> 
    <option value="Between">Between</option> 
    <option value="LessThan">Less than</option> 
</select> 
<input name="Threshold" type="text" size="4" /> 
<input name="AdditionalThreshold" type="text" id="BetweenField" size="4" class="hide"> 

<script> 
function toggleBetween(event) { 
    if(event.target.value.toLowerCase() === 'between') { 
     document.querySelector('#BetweenField').classList.remove('hide'); 
    } else { 
     document.querySelector('#BetweenField').classList.add('hide'); 
    } 
} 

document.addEventListener('DOMContentLoaded', function() { 
    document.querySelector('select[name="ThresholdType"]').addEventListener('change', toggleBetween, false); 
}) 
</script> 

<style> 
.hide { 
    display: none; 
} 
</style> 

http://jsbin.com/pigocekava/1/edit?html,css,js,output

+0

ありがとう@ジョー。これは本当にうまくいった。あなたのサポートは大変感謝しています。 – Tom

関連する問題