带有复选框的Textarea占位符

时间:2016-01-05 21:42:31

标签: javascript jquery html html5 placeholder

我试图通过按下复选框按钮来更改textarea的占位符。我无法让它改变。

要清楚,我试图设置占位符而不是值。

我的textarea:

<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>

我特别需要的 - 一个复选框按钮,用于将textarea的占位符更改为2个不同的选项。 EX:点击一次,它会显示&#34;年龄&#34;再次点击它会显示&#34; name&#34 ;,再次点击它会显示&#34; age&#34;等

1 个答案:

答案 0 :(得分: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>