2017-04-25 21 views
0

我需要改變HTML/CSS/jQuery切換元素的顯示方式 - 在「是」或「否」 - 取決於值表格中的一個單元格。使用jQuery將切換元素設置爲'開'或'關'HTML表格中的給定值

所需的功能是:

  1. 如果表格單元格具有文本,上面寫着「是」肘節式元件需要有一類.offtrue或檢查設定的複選框的選中屬性。

  2. 如果單元格的文本顯示爲「no」,則toggle元素需要具有.on的類別,並將複選框的選中屬性設置爲false或未選中。

這是到目前爲止我的代碼:

// TRIM FUNCTION 
 
if (typeof String.prototype.trim !== 'function') { 
 
    String.prototype.trim = function() { 
 
     return this.replace(/^\s+|\s+$/g, ''); 
 
    } 
 
} 
 

 
// TOGGLE FUNCTIONALITY 
 
$(document).ready(function() { 
 

 
    // FIND DEV YES/NO INPUT & CHECK VALUE 
 
    var ynCell = $("td.yn"); 
 
    $(ynCell).each(function() { 
 
     var ynValue = $(ynCell).text().toLowerCase().trim(); 
 
     // IF VALUE = NO, DISPLAY TOGGLE CLASS 'ON' 
 
     // IF VALUE = YES, DISPLAY TOGGLE CLASS 'OFF' 
 
     if (ynValue.indexOf('no') != -1) { 
 
      $(".switch").parent().find('input:checkbox').attr('checked', false); 
 
      $(".switch").removeClass('off').addClass('on'); 
 
     } else if (ynValue.indexOf('yes') != -1) { 
 
      $(".switch").parent().find('input:checkbox').attr('checked', true); 
 
      $(".switch").removeClass('on').addClass('off'); 
 
     } 
 
    }); 
 

 
    $(".switch").each(function() { 
 
     if ($(this).parent().find('input:checkbox').length) { 
 
      if (!$(this).parent().find('input:checkbox').hasClass("show")) { 
 
       $(this).parent().find('input:checkbox').hide(); 
 
      } 
 
      if ($(this).parent().find('input:checkbox').is(':checked')) { 
 
       $(this).removeClass('on').addClass('off'); 
 
      } else { 
 
       $(this).removeClass('off').addClass('on'); 
 
      } 
 
     } 
 
    }); 
 

 
    $(".switch").click(function() { 
 
     if ($(this).hasClass('on')) { 
 
      $(this).parent().find('input:checkbox').attr('checked', true); 
 
      $(this).removeClass('on').addClass('off'); 
 
     } else { 
 
      $(this).parent().find('input:checkbox').attr('checked', false); 
 
      $(this).removeClass('off').addClass('on'); 
 
     } 
 
    }); 
 

 
});
th { 
 
    text-align: left; 
 
} 
 

 
.switch-container { 
 
    padding: 5px; 
 
} 
 
.switch { 
 
    position: relative; 
 
    display: inline-block; 
 
    font-size: 1.6em; 
 
    font-weight: bold; 
 
    color: #ccc; 
 
    height: 18px; 
 
    padding: 6px 6px 5px 6px; 
 
    border: 1px solid #ccc; 
 
    border-radius: 5px; 
 
    background: #ececec; 
 
    cursor: pointer; 
 
    font-size: 14px; 
 
} 
 
body.IE7 .switch { 
 
    width: 78px; 
 
} 
 
.switch span { 
 
    display: inline-block; 
 
    width: 35px; 
 
} 
 
.switch span.on { 
 
    color: #5cbacc; 
 
    margin-left: 5px; 
 
} 
 
.switch span.off { 
 
    margin-left: 10px; 
 
} 
 
.switch .toggle { 
 
    position: absolute; 
 
    top: 1px; 
 
    width: 40px; 
 
    height: 25px; 
 
    border: 1px solid #ccc; 
 
    border-radius: 5px; 
 
    background: #fff; 
 
    z-index: 999; 
 
    -webkit-transition: all 0.15s ease-in-out; 
 
    -moz-transition: all 0.15s ease-in-out; 
 
    -o-transition: all 0.15s ease-in-out; 
 
    -ms-transition: all 0.15s ease-in-out; 
 
} 
 
.switch.on .toggle { 
 
    left: 1%; 
 
} 
 
.switch.off .toggle { 
 
    left: 56%; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<table style="width:100%"> 
 
    <tr> 
 
    <th>Yes/No</th> 
 
    <th>Toggle</th> 
 
    </tr> 
 
    <tr> 
 
    <td class="yn">Yes</td> 
 
    <td class="switch-container"> 
 
     <input type="checkbox" checked> 
 
     <div class="switch"><div class="toggle"></div> 
 
     <span class="on">YES</span><span class="off">NO</span></div></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="yn">No</td> 
 
    <td class="switch-container"> 
 
     <input type="checkbox" checked> 
 
     <div class="switch"><div class="toggle"></div> 
 
     <span class="on">YES</span><span class="off">NO</span></div></td> 
 
    </tr> 
 
</table> 
 
    <script 
 
    src="https://code.jquery.com/jquery-3.2.1.min.js" 
 
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
 
    crossorigin="anonymous"></script> 
 
</body> 
 
</html>

,關於如何提高我的劇本,並得到這個工作,任何建議都歡迎。

這裏是一個JS斌:https://jsbin.com/derevarixo/edit?html,css,js,output

+1

安置自己的代碼在這裏,不只是在遠程站點。您可以使用[Stack Snippets](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/)使其可執行。 – Barmar

+0

@Barmar好主意。更新OP! – Liz

回答

1

.each()循環中,您需要使用this訪問當前循環元素,而不是$(ynCell),它包含了所有的是/否細胞。要找到相關的.switch元素,您需要使用當前行中的.find()

// TRIM FUNCTION 
 
if (typeof String.prototype.trim !== 'function') { 
 
    String.prototype.trim = function() { 
 
     return this.replace(/^\s+|\s+$/g, ''); 
 
    } 
 
} 
 

 
// TOGGLE FUNCTIONALITY 
 
$(document).ready(function() { 
 

 
    // FIND DEV YES/NO INPUT & CHECK VALUE 
 
    var ynCell = $("td.yn"); 
 
    $(ynCell).each(function() { 
 
     var ynValue = $(this).text().toLowerCase().trim(); 
 
     var row = $(this).closest("tr"); 
 
     // IF VALUE = NO, DISPLAY TOGGLE CLASS 'ON' 
 
     // IF VALUE = YES, DISPLAY TOGGLE CLASS 'OFF' 
 
     if (ynValue.indexOf('no') != -1) { 
 
      row.find('input:checkbox').attr('checked', false); 
 
      row.find(".switch").removeClass('off').addClass('on'); 
 
     } else if (ynValue.indexOf('yes') != -1) { 
 
      row.find('input:checkbox').attr('checked', true); 
 
      row.find(".switch").removeClass('on').addClass('off'); 
 
     } 
 
    }); 
 

 
    $(".switch").each(function() { 
 
     if ($(this).parent().find('input:checkbox').length) { 
 
      if (!$(this).parent().find('input:checkbox').hasClass("show")) { 
 
       $(this).parent().find('input:checkbox').hide(); 
 
      } 
 
      if ($(this).parent().find('input:checkbox').is(':checked')) { 
 
       $(this).removeClass('on').addClass('off'); 
 
      } else { 
 
       $(this).removeClass('off').addClass('on'); 
 
      } 
 
     } 
 
    }); 
 

 
    $(".switch").click(function() { 
 
     if ($(this).hasClass('on')) { 
 
      $(this).parent().find('input:checkbox').attr('checked', true); 
 
      $(this).removeClass('on').addClass('off'); 
 
     } else { 
 
      $(this).parent().find('input:checkbox').attr('checked', false); 
 
      $(this).removeClass('off').addClass('on'); 
 
     } 
 
    }); 
 

 
});
th { 
 
    text-align: left; 
 
} 
 

 
.switch-container { 
 
    padding: 5px; 
 
} 
 
.switch { 
 
    position: relative; 
 
    display: inline-block; 
 
    font-size: 1.6em; 
 
    font-weight: bold; 
 
    color: #ccc; 
 
    height: 18px; 
 
    padding: 6px 6px 5px 6px; 
 
    border: 1px solid #ccc; 
 
    border-radius: 5px; 
 
    background: #ececec; 
 
    cursor: pointer; 
 
    font-size: 14px; 
 
} 
 
body.IE7 .switch { 
 
    width: 78px; 
 
} 
 
.switch span { 
 
    display: inline-block; 
 
    width: 35px; 
 
} 
 
.switch span.on { 
 
    color: #5cbacc; 
 
    margin-left: 5px; 
 
} 
 
.switch span.off { 
 
    margin-left: 10px; 
 
} 
 
.switch .toggle { 
 
    position: absolute; 
 
    top: 1px; 
 
    width: 40px; 
 
    height: 25px; 
 
    border: 1px solid #ccc; 
 
    border-radius: 5px; 
 
    background: #fff; 
 
    z-index: 999; 
 
    -webkit-transition: all 0.15s ease-in-out; 
 
    -moz-transition: all 0.15s ease-in-out; 
 
    -o-transition: all 0.15s ease-in-out; 
 
    -ms-transition: all 0.15s ease-in-out; 
 
} 
 
.switch.on .toggle { 
 
    left: 1%; 
 
} 
 
.switch.off .toggle { 
 
    left: 56%; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<table style="width:100%"> 
 
    <tr> 
 
    <th>Yes/No</th> 
 
    <th>Toggle</th> 
 
    </tr> 
 
    <tr> 
 
    <td class="yn">Yes</td> 
 
    <td class="switch-container"> 
 
     <input type="checkbox" checked> 
 
     <div class="switch"><div class="toggle"></div> 
 
     <span class="on">YES</span><span class="off">NO</span></div></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="yn">No</td> 
 
    <td class="switch-container"> 
 
     <input type="checkbox" checked> 
 
     <div class="switch"><div class="toggle"></div> 
 
     <span class="on">YES</span><span class="off">NO</span></div></td> 
 
    </tr> 
 
</table> 
 
    <script 
 
    src="https://code.jquery.com/jquery-3.2.1.min.js" 
 
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
 
    crossorigin="anonymous"></script> 
 
</body> 
 
</html>