带有复选框的文本区域占位符

Textarea placeholder with a checkbox

我正在尝试通过按下复选框按钮来更改文本区域的占位符。我只是无法改变它。

明确地说,我试图设置一个占位符而不是一个值。

我的文本区:

<textarea name="problem" id="problem" rows="3" class="form-control" cols="45" placeholder="Describe your ticket reason in as few words as possible."></textarea>

我的JavaScript:

<div class="onoffswitch">
  <input type="checkbox" onchange="changeplh()" name="onoffswitch" class="onoffswitch-checkbox" value="1" id="myonoffswitch" checked>
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
  <script>
    function changeplh() {
      debugger;
      var sel = document.getElementById("myonoffswitch");
      var textbx = document.getElementById("problem");
      var indexe = sel.selectedIndex;

      if (indexe == 0) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "age");

      }
      if (indexe == 1) {
        $("#problem").val('');
        $("#problem").prop("placeholder", "name");
      }
    }
  </script>
</div>

我特别需要的 - 一个复选框按钮,可将文本区域的占位符更改为 2 个不同的选项。 例如:点击一次,显示“年龄”,再次点击显示“姓名”,再次点击显示“年龄”等

您的代码有点乱(例如,您的代码中没有 #problem)所以我只是创建了一个更改 placeholder.

的简单演示

$('#problem').change(function() {
  $('textarea').attr('placeholder', $(this).is(':checked') ? 'name' : 'age');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="form-control" style="height:150px" name="techProblem" placeholder="Describe the issue in as few words as possible."></textarea>

<label><input type="checkbox" id="problem" /> Change placeholder</label>