2016-11-16 69 views
0

我有三個表單域,文本框,下拉列表和複選框。我想將下拉列表重置爲第一項,並選中複選框以取消選中是否單擊重置。我試過這段代碼,但它只會重置文本框而不是下拉列表。我需要一些重置複選框。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 =「General」,ErrorMessageId =「RequiredField」)] [DropdownList(「ProductType」)] public string Type {get;組; } –

回答

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

第二個例子顯示了答案。您需要爲具有默認值的