使用切换功能更改文字

时间:2017-01-31 19:25:57

标签: javascript jquery function toggle

我的复选标记输入有一个切换功能来显示/隐藏输入框,但我想要做的是获得两个不同的文本选项以淡入切换选项(显然,因为复选标记是真的页面加载,以便在加载时显示相应的文本。)

当复选框输入为true时,我想要它说Enabled

当复选框输入为false时,我希望它说Turned Off

我该怎么做?



// Checkbox checked and input disbaled when page loads
    $('#TYemailCheck').prop('checked', true);

    // Enable-Disable text input when checkbox is checked or unchecked
    function TYenable(trigger, target) {
        $(trigger).on('change', function () {
            $(target).toggle();
        });
    }
    TYenable("#TYemailCheck", "#TYemailEnabled");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="TYemailCheck">
<div id="TYemailEnabled">
     <input type="text">
 </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

根据divs更改,您应该包含两个toggle包含您的文本和checkbox

&#13;
&#13;
// Checkbox checked and input disbaled when page loads
$('#TYemailCheck').prop('checked', true);
$('#turnedOff').hide();
// Enable-Disable text input when checkbox is checked or unchecked
function TYenable(trigger, target) {
      $(trigger).on('change', function () {
          $(target).toggle();
          $('#enabled, #turnedOff').fadeToggle(500);
      });
}
TYenable("#TYemailCheck", "#TYemailEnabled");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="TYemailCheck"><span id="enabled">Enabled</span><span id="turnedOff">Turned Off</span>
<div id="TYemailEnabled">
     <input type="text">
 </div>
&#13;
&#13;
&#13;