【问题标题】:Set the Dropdown list value as per the input field根据输入字段设置下拉列表值
【发布时间】:2015-11-20 05:46:03
【问题描述】:

我是 JavaScript 新手。我将下拉列表值推送到输入字段中。我正在成功地做到这一点。当我尝试在下拉列表中已经存在的输入字段中输入一个值时,函数调用会显示正确的值。部门标签下拉列表更改,但子类别 div 下拉列表值不会更改,直到我单击该 div。怎么能改变呢? 任何帮助将不胜感激

function fields() {
    var seldprt = document.getElementById("seldprt");
    var section = seldprt.value;
    var dprt_input = section;
    var input_Department = document.getElementById("departmentinput");
    input_Department.value = dprt_input;  
  
    if (section == "Finance") {
        document.getElementById("FinanceDiv").style.display = "";
        document.getElementById("SalesDiv").style.display = "none";
        document.getElementById("ITDiv").style.display = "none";
        var subsection = document.getElementById("FinanceDiv_subcatagory");
        var input_Subcatagory = document.getElementById("subcatagoryinput");
        input_Subcatagory.value = sub_catagory_input;
    } else if (section == "Sales")	{	
        document.getElementById("SalesDiv").style.display = "";
        document.getElementById("FinanceDiv").style.display = "none";
        document.getElementById("ITDiv").style.display = "none";
        var subsection = document.getElementById("SalesDiv_subcatagory");
        var sub_catagory_input = subsection.value;
        var input_Subcatagory = document.getElementById("subcatagoryinput");
        input_Subcatagory.value=sub_catagory_input;
			 
    } else if (section == "IT")	{	
        document.getElementById("ITDiv").style.display = "";
        document.getElementById("SalesDiv").style.display = "none";
        document.getElementById("FinanceDiv").style.display = "none";
        var subsection = document.getElementById("ITDiv_subcatagory");
        var sub_catagory_input = subsection.value;
        var input_Subcatagory = document.getElementById("subcatagoryinput");
        input_Subcatagory.value=sub_catagory_input;	 
    }				
}

function previous_values() {
    var input_Department = document.getElementById("departmentinput").value;
    var input_Subcatagory = document.getElementById("subcatagoryinput").value;
    alert(input_Department+","+input_Subcatagory);
    //seldprt.value = input_Department;	
    if(input_Department != null) {   
        alert();
        var seldprt = document.getElementById("seldprt");
        seldprt.value = input_Department;	
        var Finance_subsection = document.getElementById("FinanceDiv_subcatagory");
        var Sales_subsection = document.getElementById("SalesDiv_subcatagory");
        var ITDiv_subsection = document.getElementById("ITDiv_subcatagory");
        if (input_Department == "Finance") {
            document.getElementById("FinanceDiv_subcatagory").value = input_Subcatagory;
            //Finance_subsection.value = input_Subcatagory;
            //alert(Finance_subsection+"Finance_subsection");		
        }
        if (input_Department == "Sales") {
            document.getElementById("SalesDiv_subcatagory").value = input_Subcatagory;	
            //alert(Sales_subsection.value+"subsection value");
        }	
        if(input_Department=="IT") {
            document.getElementById("ITDiv_subcatagory").value = input_Subcatagory;
            //alert(ITDiv_subsection+"subsection value");
        }
        //alert("if block"+seldprt.value+","+Finance_subsection.value+","+Sales_subsection.value+","+ITDiv_subsection.value);
									
    } /* else {
        var seldprt = document.getElementById("seldprt");
        seldprt.value = input_Department;
        alert("else block"+seldprt.value);
        var Finance_subsection = document.getElementById("FinanceDiv_subcatagory");
        Finance_subsection.value = input_Department;
        var Sales_subsection = document.getElementById("SalesDiv_subcatagory");
        Sales_subsection.value = input_Department;		
        var ITDiv_subsection = document.getElementById("ITDiv_subcatagory");
        ITDiv_subsection.value = input_Department;
        alert("else block");
			
    } */
}	 
<body onload="preparePage();fields();previous_values();" >
	<div class="title"><%=getUserName(request)%>'s Emergency Contact</div><hr/>
	<div class="instructions">Please provide the name and telephone number of a person that we may contact in case of an emergency.</div>
<div class="container" onclick="fields()">
<div onchange = "" class="departmentdiv">
  <label>Department:</label> 
  <div align="right" class="selectdiv">
  <select id = "seldprt">
   <option value = "Finance">Finance</option>
   <option value = "Sales">Sales</option>
   <option value = "IT">IT</option>   
  </select>
  </div>
 </div>

 
  
 <div id="FinanceDiv" onclick="" class="subcatagorydiv" >
  <label>Section Name:</label>
  <div align="right" class="selectdiv">
  <select id = "FinanceDiv_subcatagory">
   <option value = "Reimbursment">Reimbursment</option>
   <option value = "Billing">Billing</option>
   <option value = "Salaries">Salaries</option>
  </select>
  </div>
 
</div>
<div id="SalesDiv" onclick="previous_values()" class="subcatagorydiv" style="display:none;">
  <label>Section Name:</label>
  <div align="right" class="selectdiv">
  <select id = "SalesDiv_subcatagory">
   <option value = "Total Sales">Total Sales</option>
   <option value = "Purchases">Purchases</option>
   <option value = "BestProduct">BestProduct</option>
  </select>
  </div>
</div>

<div id="ITDiv" onclick="previous_values()" class="subcatagorydiv" style="display:none;" >
  <label>Section Name:</label>
  <div align="right" class="selectdiv">
  <select id = "ITDiv_subcatagory">
   <option value = "Internee">Internee</option>
   <option value = "Associates">Associates</option>
   <option value = "Managers">Managers</option>
  </select>
  </div>
</div>
</div>


		<form action="" method="post" >
			<div class="entry" onclick="previous_values()">Name</div>
			<input type="text" name="name" id="departmentinput">
			<div class="entry" onchange="previous_values()">Telephone Number</div>
			<input type="text" name="telephone" id="subcatagoryinput">
			<div id="readwrite_buttons" class="hide">
				<button id="ok" onclick="document.forms[0].submit();return false;">OK</button>
				<button id="cancel" onclick="javascript:window.close();return false;">Cancel</button>
			</div>
			<div id="readonly_buttons" class="hide">
				<button id="back" onclick="javascript:window.close();return false;">Back</button>
			</div>
		</form>
	</body>

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您需要在&lt;select&gt; 元素(seldprt)上连接onchange 事件以调用fields()。另外 - onchange 事件不适用于 div 元素。

    这是因为您已经编写了fields() 来读取新值并重新渲染 DOM。老实说,我没有阅读其余的代码。

    您可能为自己的目的编写了太多代码,但这没关系,因为您仍在学习。我建议用最少的功能重写,验证每个部分是否有效,然后一次添加更多功能。

    查看固定的sn-p:

    function fields() {
        var seldprt = document.getElementById("seldprt");
        var section = seldprt.value;
        var dprt_input = section;
        var input_Department = document.getElementById("departmentinput");
        input_Department.value = dprt_input;  
      
        if (section == "Finance") {
            document.getElementById("FinanceDiv").style.display = "";
            document.getElementById("SalesDiv").style.display = "none";
            document.getElementById("ITDiv").style.display = "none";
            var subsection = document.getElementById("FinanceDiv_subcatagory");
            var input_Subcatagory = document.getElementById("subcatagoryinput");
            input_Subcatagory.value = sub_catagory_input;
        } else if (section == "Sales")	{	
            document.getElementById("SalesDiv").style.display = "";
            document.getElementById("FinanceDiv").style.display = "none";
            document.getElementById("ITDiv").style.display = "none";
            var subsection = document.getElementById("SalesDiv_subcatagory");
            var sub_catagory_input = subsection.value;
            var input_Subcatagory = document.getElementById("subcatagoryinput");
            input_Subcatagory.value=sub_catagory_input;
    			 
        } else if (section == "IT")	{	
            document.getElementById("ITDiv").style.display = "";
            document.getElementById("SalesDiv").style.display = "none";
            document.getElementById("FinanceDiv").style.display = "none";
            var subsection = document.getElementById("ITDiv_subcatagory");
            var sub_catagory_input = subsection.value;
            var input_Subcatagory = document.getElementById("subcatagoryinput");
            input_Subcatagory.value=sub_catagory_input;	 
        }				
    }
    
    function previous_values() {
        var input_Department = document.getElementById("departmentinput").value;
        var input_Subcatagory = document.getElementById("subcatagoryinput").value;
        alert(input_Department+","+input_Subcatagory);
        //seldprt.value = input_Department;	
        if(input_Department != null) {   
            alert();
            var seldprt = document.getElementById("seldprt");
            seldprt.value = input_Department;	
            var Finance_subsection = document.getElementById("FinanceDiv_subcatagory");
            var Sales_subsection = document.getElementById("SalesDiv_subcatagory");
            var ITDiv_subsection = document.getElementById("ITDiv_subcatagory");
            if (input_Department == "Finance") {
                document.getElementById("FinanceDiv_subcatagory").value = input_Subcatagory;
                //Finance_subsection.value = input_Subcatagory;
                //alert(Finance_subsection+"Finance_subsection");		
            }
            if (input_Department == "Sales") {
                document.getElementById("SalesDiv_subcatagory").value = input_Subcatagory;	
                //alert(Sales_subsection.value+"subsection value");
            }	
            if(input_Department=="IT") {
                document.getElementById("ITDiv_subcatagory").value = input_Subcatagory;
                //alert(ITDiv_subsection+"subsection value");
            }
            //alert("if block"+seldprt.value+","+Finance_subsection.value+","+Sales_subsection.value+","+ITDiv_subsection.value);
    									
        } /* else {
            var seldprt = document.getElementById("seldprt");
            seldprt.value = input_Department;
            alert("else block"+seldprt.value);
            var Finance_subsection = document.getElementById("FinanceDiv_subcatagory");
            Finance_subsection.value = input_Department;
            var Sales_subsection = document.getElementById("SalesDiv_subcatagory");
            Sales_subsection.value = input_Department;		
            var ITDiv_subsection = document.getElementById("ITDiv_subcatagory");
            ITDiv_subsection.value = input_Department;
            alert("else block");
    			
        } */
    }	 
    <body onload="preparePage();fields();previous_values();" >
    	<div class="title"><%=getUserName(request)%>'s Emergency Contact</div><hr/>
    	<div class="instructions">Please provide the name and telephone number of a person that we may contact in case of an emergency.</div>
    <div class="container" onclick="fields()">
    <div class="departmentdiv">
      <label>Department:</label> 
      <div align="right" class="selectdiv">
      <select onchange = "fields()" id = "seldprt">
       <option value = "Finance">Finance</option>
       <option value = "Sales">Sales</option>
       <option value = "IT">IT</option>   
      </select>
      </div>
     </div>
    
     
      
     <div id="FinanceDiv" onclick="" class="subcatagorydiv" >
      <label>Section Name:</label>
      <div align="right" class="selectdiv">
      <select id = "FinanceDiv_subcatagory">
       <option value = "Reimbursment">Reimbursment</option>
       <option value = "Billing">Billing</option>
       <option value = "Salaries">Salaries</option>
      </select>
      </div>
     
    </div>
    <div id="SalesDiv" onclick="previous_values()" class="subcatagorydiv" style="display:none;">
      <label>Section Name:</label>
      <div align="right" class="selectdiv">
      <select id = "SalesDiv_subcatagory">
       <option value = "Total Sales">Total Sales</option>
       <option value = "Purchases">Purchases</option>
       <option value = "BestProduct">BestProduct</option>
      </select>
      </div>
    </div>
    
    <div id="ITDiv" onclick="previous_values()" class="subcatagorydiv" style="display:none;" >
      <label>Section Name:</label>
      <div align="right" class="selectdiv">
      <select id = "ITDiv_subcatagory">
       <option value = "Internee">Internee</option>
       <option value = "Associates">Associates</option>
       <option value = "Managers">Managers</option>
      </select>
      </div>
    </div>
    </div>
    
    
    		<form action="" method="post" >
    			<div class="entry" onclick="previous_values()">Name</div>
    			<input type="text" name="name" id="departmentinput">
    			<div class="entry" onchange="previous_values()">Telephone Number</div>
    			<input type="text" name="telephone" id="subcatagoryinput">
    			<div id="readwrite_buttons" class="hide">
    				<button id="ok" onclick="document.forms[0].submit();return false;">OK</button>
    				<button id="cancel" onclick="javascript:window.close();return false;">Cancel</button>
    			</div>
    			<div id="readonly_buttons" class="hide">
    				<button id="back" onclick="javascript:window.close();return false;">Back</button>
    			</div>
    		</form>
    	</body>

    【讨论】:

    • 感谢杰夫的输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多