2016-11-16 4 views
0

私は3つのフォームフィールド、テキストボックス、ドロップダウンリストとチェックボックスを持っています。最初の項目にドロップダウンリストをリセットし、リセットをクリックするとチェックボックスをオフにします。私はこのコードを試しましたが、ドロップダウンリストではなくテキストボックスのみをリセットします。チェックボックスをリセットする必要があります。Javascriptリセットドロップダウンリストとチェックボックス

注:ドロップダウンリストタイプはモデルで定義されています。チェックボックスをオフにするために

<input type="text" name="a" class="input-medium w1" data-bind="value: value" /> 

<label class="control-label" for="category" id="ProductType">Type:&nbsp; @Html.EditorFor(m => m.Type)</label> 

<label class="control-label">Included Discontinued:@Html.CheckBox("searchDate")</label> 


    var dropDown = document.getElementById("ProductType"); 
     dropDown.selectedIndex = 0; 

    var elements = document.getElementsByTagName("input"); 
    for (var ii = 0; ii < elements.length; ii++) { 
     if (elements[ii].type == "text") { 
      elements[ii].value = ""; 
     }  
    } 
+0

'#ProductType'は、ラベルではなく、ドロップダウンのように見えますか? – adeneo

+0

これは{取得[RequiredValidator(ErrorMessageCategory = "一般的"、ErrorMessageId = "RequiredField")] [ドロップダウンリスト( "でProductType")] パブリック文字列型ドロップダウン・リストです。セット; } –

答えて

0

は、リストの使用をドロップダウン

の場合

document.getElementById("ID").checked = false; 

例えば

<!DOCTYPE html> 
<html> 
<body> 

<form> 
    What color do you prefer?<br> 
    <input type="radio" name="colors" id="red">Red<br> 
    <input type="radio" name="colors" id="blue">Blue 
</form> 

<button onclick="check()">Check "Red"</button> 
<button onclick="uncheck()">Uncheck "Red"</button> 

<script> 
function check() { 
    document.getElementById("red").checked = true; 
} 
function uncheck() { 
    document.getElementById("red").checked = false; 
} 
</script> 

</body> 
</html> 

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_radio_checked2

を使用します

document.getElementById("orange").selected = "true"; 
例えば

<!DOCTYPE html> 
<html> 
<body> 

<form> 
Select your favorite fruit: 
<select> 
    <option id="apple">Apple</option> 
    <option id="orange">Orange</option> 
    <option id="pineapple" selected>Pineapple</option> // Pre-selected 
    <option id="banana">Banana</option> 
</select> 
</form> 

<p>Click the button to change the selected option in the dropdown list to "orange" instead of "pinapple".</p> 

<button onclick="myFunction()">Try it</button> 

<script> 
function myFunction() { 
    document.getElementById("orange").selected = "true"; 
} 
</script> 

</body> 
</html> 

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_option_selected2

+0

私はすでにドロップダウンリストに値を持っています。私はこの関数を試しました。Reset(){ document.getElementById( "red")。checked = false; document.getElementById( "select")。selectedIndex = 0; } –

+0

チェックボックスのみが機能します –

+0

2番目の例は答えを示しています。 idをデフォルト値の

関連する問題