选择下拉菜单时重置隐藏字段的值(Javascript)

选择下拉菜单时重置隐藏字段的值(Javascript),javascript,html,forms,drop-down-menu,hidden,Javascript,Html,Forms,Drop Down Menu,Hidden,获得了一个带有div下拉列表的表单,选择该下拉列表时,将显示/隐藏另一个div <div> <select id="currency" name="currency" onChange="display_payroll_div(this.selectedIndex);"> <option value="0">Currency for your quote*</option> <option value=

获得了一个带有div下拉列表的表单,选择该下拉列表时,将显示/隐藏另一个div

<div>
    <select id="currency" name="currency" onChange="display_payroll_div(this.selectedIndex);">
        <option value="0">Currency for your quote*</option>
        <option value="AUD">AUD</option>
        <option value="USD">USD</option>
        <option value="Pounds">GBP</option>
    </select>
</div>
<div id="payroll-div">
    <select id="report-payroll" name="report-payroll">
        <option value="0">Would you like us to do payroll?*</option>
        <option value="1">Yes, I would like payroll</option>
        <option value="2">No, I don't need payroll</option>
    </select>
</div>
显示/隐藏很好,但问题是,当我选择“AUD”和“是,我想要工资单”,然后改变主意并更改为“USD”时,隐藏字段工资单仍将保留“是,…”的值。是否需要将“工资单部门”重置为默认值

我看到的所有建议都使用了“onClick”,我尽量避免使用它,因为它在FF中不起作用。任何帮助都将不胜感激:)

请尝试以下方法:

function display_payroll_div(e){
    currency_value=document.getElementById('currency').value;
    document.getElementById('payroll-div').style.display = "none";
    if (currency_value == 'AUD') {
        document.getElementById('payroll-div').style.display = "block";
    }
    else {
        document.getElementById('report-payroll').selectedIndex = 0;
    }
}

参考资料:

在代码中添加以下内容:

else
{
 document.getElementById('report-payroll').selectedIndex = 0;
 //reseting value when currency_value != 'AUD'
}

如下更改函数

  function display_payroll_div(e){
      currency_value=document.getElementById('currency').value;
      document.getElementById('payroll-div').style.display = "none";
       if (currency_value == 'AUD') {
              document.getElementById('payroll-div').style.display = "block";
        } else {
        document.getElementById('report-payroll').value ='0';
      }
   }
我建议:

1) 不引人注目的
2) 可以处理重新加载

<select id="currency" name="currency">
你可以用

document.getElementById('report-payroll').options[0].selected = true;

无论如何,您应该使用onchange。但是创建一个jsfiddle.net,这样我们就不必这样做了。您的意思是
document.getElementById(“报告工资单”)。隐藏时选择的索引=0
window.onload=function() {
  document.getElementById("currency").onchange=function(){
    var currency_value = this.value, 
        payrollDiv     = document.getElementById('payroll-div');
    payrollDiv.style.display = currency_value=="AUD"?"block":"none";
    if (currency_value!="AUD") document.getElementById("report-payroll").selectedIndex=0;
  }
  document.getElementById("currency").onchange(); // hide or show when loading
}
document.getElementById('report-payroll').options[0].selected = true;