gpt4 book ai didi

javascript - 传递两个 document.getElementById 不起作用

转载 作者:行者123 更新时间:2023-11-28 07:19:16 25 4
gpt4 key购买 nike

所以我试图传递两个 update(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>

最佳答案

修复

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 block 。如果您在编程上做了错误的事情,那么就会发现。

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);
}
}

关于javascript - 传递两个 document.getElementById 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30491323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com