选择下拉列表时文本更改无效

时间:2016-07-16 09:13:21

标签: jquery html

我编写了一个脚本,我需要根据dropdown的选择更改标签名称。

但问题是标签没有出现在dropdown的选择上,我需要一些帮助。

如果我在某个地方出错了,请原谅我。谢谢。!

label.html

<div class="form-group">
    <select id="json-one" class="form-control lable">  
        <option selected="" value="base">Please Select</option>  
        <option value="Doctor">Doctor</option>  
        <option value="Lawyer">Lawyer</option>  
    </select>  
</div>
<div class="form-group"><label id="labelChange"></label>
    <input type="text" class="form-control" required="" name="lname" minlength="2">
</div>

//.js script

<script>
    var newLabel = '';
$('.lable').on('change', function(){
    $('#labelChange').text(newLabel); //Change the text before changing the value
    switch(this.value){
        case 'Doctor':
            newLabel = 'Clinic name';
            break;
        case 'Lawyer':
            newLabel = 'Practice address';
            break;

    }
}).trigger('change');
    </script>

2 个答案:

答案 0 :(得分:0)

  

您的问题是newLabel变量。

     

在切换案例后添加$('#labelChange').text(newLabel);

&#13;
&#13;
    var newLabel = '';
$('.lable').on('change', function(){
    switch(this.value){
        case 'Doctor':
            newLabel = 'Clinic name';
            break;
        case 'Lawyer':
            newLabel = 'Practice address';
            break;

    }
   $('#labelChange').text(newLabel); //Change the text before changing the value
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
    <select id="json-one" class="form-control lable">  
        <option selected="" value="base">Please Select</option>  
        <option value="Doctor">Doctor</option>  
        <option value="Lawyer">Lawyer</option>  
    </select>  
</div>
<div class="form-group"><label id="labelChange"></label>
    <input type="text" class="form-control" required="" name="lname" minlength="2">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试这样的事情让我知道

$(document).ready(function () {

    $("#json-one").change(function () {

        var newLabel = '';

        switch (this.value) {
            case 'Doctor':
                newLabel = 'Clinic name';
                break;
            case 'Lawyer':
                newLabel = 'Practice address';
                break;    
        }

        $("#labelChange").html(newLabel);
    });
});