通过两个document.getElemenyById不工作

时间:2015-05-27 19:20:10

标签: javascript

所以我试图通过两次更新(document.getElementById());.由于某种原因,它只通过第一个而忽略第二个。

function updateDays() {                                           
    var vacationDays = $('#vacationDays').val().split(",").length;
    $('#vacationDaysTaken').val(vacationDays);                    
    update(document.getElementById('vacationDaysTaken'));         

    var totalDaysOff = $('#daysOff').val().split(",").length;     
    $('#totalDaysOff').val(totalDaysOff);                         
    update(document.getElementById('totalDaysOff'));              
}                                                                 

function update(obj) {                 
    if(obj.type == "checkbox") {       
        if(obj.checked)                
            changeList[obj.id] = "on"; 
        else                           
            changeList[obj.id] = "";   
    }                                  
    else {                             
        changeList[obj.id] = obj.value;
    }                                  
    updateDays();                      
}     

HTML:

    <body>                                                                                                               
    <div id="wrapper">                                                                                               
        <div id="content">                                                                                           
            <h1>Paid Vacation Request</h1>                                                                           

            <div id="vacBorder">                                                                                     
                <span id="msg" style="color: red; font-weight: bold">&nbsp;</span>                                   
                <table id="vac">                                                                                     
                    <tr>                                                                                             
                        <th>Employee:</th>                                                                           
                        <td><span id="empName"></span></td>                                                          
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Request Date:</th>                                                                       
                        <td><span id="requestDate"></span></td>                                                      
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Days Requesting Off:</th>                                                                
                        <td><input type="text" id="daysOff" name="Days Off" required></td>                           
                   </tr>                                                                                             
                   <tr>                                                                                              
                        <th>Vacation Days Requeted:</th>                                                             
                        <td><input type="text" id="vacationDays" name="Vacation Days" required></td>                 
                   </tr>                                                                                             
                    <tr>                                                                                             
                        <th>Total Days Off:</th>                                                                     
                        <td><input type="int" id="totalDaysOff" name="Total Days Off"></td>                          
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Vacation Days Taken:</th>                                                                
                        <td><input type="int" id="vacationDaysTaken" class="vacDays" name="Vacation Days Taken"></td>
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Total Days Taken:</th>                                                                   
                        <td><input type="int" id="numDaysTakenTotal" name="# of days taken total" readonly></td>     
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Days Left:</th>                                                                          
                        <td><input type="int" id="numDaysLeft" name="# of days left" readonly></td>                  
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Employee Comments:</th>                                                                  
                        <td><span id="employeeComments"></span></td>                                                 
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Manager Comments:</th>                                                                   
                        <td><textarea style="width: 300px;" id="managerComments"></textarea></td>                    
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Manager Approved:</th>                                                                   
                        <td>                                                                                         
                            <input type="checkbox" id="managerApproved">                                             
                            <span id="managerApprovedBy"></span>                                                     
                        </td>                                                                                        
                    </tr>                                                                                            
                    <tr>                                                                                             
                        <th>Status</th>                                                                              
                        <td><span id="status"></td>                                                                  
                    </tr>                                                                                            
                </table>                                                                                             
                <input type="button" value="Submit" onclick="submitVac();">                                          
                <input type="button" value="Deny Request" onclick="denyVac();">                                      
                <input type="button" value="Cancel Request" onclick="cancelVac();" id="cancelVac">                   
            </div>                                                                       
</div>
</div>
</body>

1 个答案:

答案 0 :(得分:0)

<强>修正

function update(obj) {                 
    if(obj.type == "checkbox") {       
        if(obj.checked)                
            changeList[obj.id] = "on"; 
        else                           
            changeList[obj.id] = "";   
    }                                  
    else {                             
        changeList[obj.id] = obj.value;
    }                                  
}              
updateDays();//move this outside of the function

附加

如果您仍然遇到问题,请更新updateDays()以使用try catch块。如果你在程序上做错了,那就会发现。

function updateDays() {                                           
    var vacationDays = $('#vacationDays').val().split(",").length;
    $('#vacationDaysTaken').val(vacationDays);             
    try {       
      update(document.getElementById('vacationDaysTaken')); 
    } catch (e) {
      alert('error - check your console');
      console.log(e);
    }
    var totalDaysOff = $('#daysOff').val().split(",").length;     
    $('#totalDaysOff').val(totalDaysOff);                  
    try {                          
       update(document.getElementById('totalDaysOff'));    
    } catch (e) {
      alert('error - check your console');
      console.log(e);
    }           
}