这个Javascript函数可以在jQuery中重新编写吗?
我有一个与Cold Fusion集成的遗留JS函数,它可以输出封装在if语句中的第n个ID表单字段。有时输出相当高,看起来非常冗余和繁琐。我认为for循环可以解决这个问题,但不确定正确的解决方法。下面是示例输出中的遗留代码。感谢您的帮助:这个Javascript函数可以在jQuery中重新编写吗?,javascript,jquery,Javascript,Jquery,我有一个与Cold Fusion集成的遗留JS函数,它可以输出封装在if语句中的第n个ID表单字段。有时输出相当高,看起来非常冗余和繁琐。我认为for循环可以解决这个问题,但不确定正确的解决方法。下面是示例输出中的遗留代码。感谢您的帮助: function checkFinalDisposition(){ if (document.getElementById("mrostatus").value == "Completed"){ var checkfinal = document.
function checkFinalDisposition(){
if (document.getElementById("mrostatus").value == "Completed"){
var checkfinal = document.getElementById("finaldisposition").value
if (checkfinal == 'NEGATIVE' || checkfinal == 'NEGATIVE DILUTE'){
var checkfinalpass = 'Yes'
if (document.getElementById("id4735721").value != 'NEGATIVE' || document.getElementById("id4735721").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735722").value != 'NEGATIVE' || document.getElementById("id4735722").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735723").value != 'NEGATIVE' || document.getElementById("id4735723").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735724").value != 'NEGATIVE' || document.getElementById("id4735724").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735725").value != 'NEGATIVE' || document.getElementById("id4735725").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735726").value != 'NEGATIVE' || document.getElementById("id4735726").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
if (document.getElementById("id4735727").value != 'NEGATIVE' || document.getElementById("id4735727").value != 'NEGATIVE DILUTE'){
var checkfinalpass = 'No'
}
}
else{
var checkfinalpass = 'No'
//Multiple IF's from drug query
if (document.getElementById("id4735721").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735722").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735723").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735724").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735725").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735726").value == checkfinal){
var checkfinalpass = 'Yes'
}
if (document.getElementById("id4735727").value == checkfinal){
var checkfinalpass = 'Yes'
}
}
if (checkfinalpass != 'Yes'){
var inputfinaldisposition = 'NEGATIVE'
//Multiple IF's from drug query
if (document.getElementById("id4735721").value == ""){
document.getElementById("id4735721").value = "POSITIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735721").value != 'NEGATIVE' || document.getElementById("id4735721").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735721").value
}
if (document.getElementById("id4735722").value == ""){
document.getElementById("id4735722").value = "POSITIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735722").value != 'NEGATIVE' || document.getElementById("id4735722").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735722").value
}
if (document.getElementById("id4735723").value == ""){
document.getElementById("id4735723").value = "NEGATIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735723").value != 'NEGATIVE' || document.getElementById("id4735723").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735723").value
}
if (document.getElementById("id4735724").value == ""){
document.getElementById("id4735724").value = "NEGATIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735724").value != 'NEGATIVE' || document.getElementById("id4735724").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735724").value
}
if (document.getElementById("id4735725").value == ""){
document.getElementById("id4735725").value = "NEGATIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735725").value != 'NEGATIVE' || document.getElementById("id4735725").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735725").value
}
if (document.getElementById("id4735726").value == ""){
document.getElementById("id4735726").value = "NEGATIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735726").value != 'NEGATIVE' || document.getElementById("id4735726").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735726").value
}
if (document.getElementById("id4735727").value == ""){
document.getElementById("id4735727").value = "NEGATIVE";
}
if ((inputfinaldisposition == 'NEGATIVE' || inputfinaldisposition == 'NEGATIVE DILUTE') && (document.getElementById("id4735727").value != 'NEGATIVE' || document.getElementById("id4735727").value != 'NEGATIVE DILUTE')){
var inputfinaldisposition = document.getElementById("id4735727").value
}
var answer = confirm("Please advise the Final Disposition does not match your individual results!\n\nPress \"OK\" to change Final Disposition to "+inputfinaldisposition)
if (answer){
document.getElementById("finaldisposition").value = inputfinaldisposition;
return true;
}
else{
return false
}
}
}
}
我会有一个js的ID数组,你正在检查
idToCheck = ['id4735721', 'id4735722', ...];
然后在for循环中比较它们
for(id in idToCheck){
if($('#'+idToCheck[id]).val() != 'NEGATIVE' || if($('#'+idToCheck[id]).val() != 'NEGATIVE DILUTE'){
checkfinalpass = 'No';
}
}
我没有重做您的全部代码,但希望这将为您指明正确的方向。jQuery是JavaScript。但是,您可以使用jQuery甚至普通的旧javascript函数来缩短一些代码。回答这个问题:Yes.js id对象实际上是一个数组,但是Yes这会缩短它并使它更易于维护。您是对的,会解决这个问题吗