【问题标题】:how to change the label text present inside a div? and how to change the dropdown to a textbox?如何更改 div 内的标签文本?以及如何将下拉菜单更改为文本框?
【发布时间】:2015-08-31 10:58:25
【问题描述】:

我有以下 html

<div class="field">
    <label for="approval_approvers_attributes_0_approver_type">Approver</label>
    <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_type" name="approval[approvers_attributes][0][approver_type]" onchange="$.fn.myFunc(this);">
        <option selected="selected" value="Role">Role</option>
        <option value="Specific User">Specific User</option>
    </select>
    </br>
    </br>
</div>
<div class="field" class="role">
    <label for="approval_approvers_attributes_0_approver_value">Role</label>
    <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]">
        <option value="hr manager">hr manager</option>
        <option selected="selected" value="reporting manager">reporting manager</option>
    </select>
    <input id="approval_approvers_attributes_0__destroy" name="approval[approvers_attributes][0][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> 
    </br>
    </br>
</div>

以及下面的js函数定义:

$.fn.myFunc = function (button) {    
    var id = $(button).attr('id');
    id = button.id;    
    $(button).next().label.text("specific user");
}

对上述 Jquery 函数的调用是在第一个 div 中进行的。当第一个 div 中的下拉菜单发生更改时,将调用此函数。在这个函数中,当用户选择"specific user" 时,我希望将标签文本更改为出现在第二个 div 中的"specific user"。因此,我想将第二个 div 中存在的下拉列表更改为文本框,以输入特定的用户名。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript jquery drop-down-menu textbox label


    【解决方案1】:

    您需要更改您的代码,例如,

    <div class="field" class="role">
        <label for="approval_approvers_attributes_0_approver_value">Role</label>
        <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]">
            <option value="hr manager">hr manager</option>
            <option selected="selected" value="reporting manager">reporting manager</option>
        </select>
        <input type="text" class="inpt_specific_user" style="display:none"/>
        <input id="approval_approvers_attributes_0__destroy" name="approval[approvers_attributes][0][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> 
        </br>
        </br>
    </div>
    
    <script>
        $.fn.myFunc = function (button) {    
            var id = $(button).attr('id');
            id = button.id;    
            var $nextDiv = $(button).closest('div.field') // get closest parent field
                                    .next('div.field') // get next parent field
             $nextDiv.find('label') // find label
                     .text(button.value); //change text, this.value so that it will be dynamic for both the values    
             $nextDiv.find('select.appr_type,input.inpt_specific_user')
                     .toggle(); // toggle select/input =>hide/show
    
        }
    </script>
    

    $.fn.myFunc = function(button) {
      var id = $(button).attr('id');
      id = button.id;
      var $nextDiv = $(button).closest('div.field') // get closest parent field
        .next('div.field') // get next parent field
      $nextDiv.find('label') // find label
        .text(button.value); //change text, this.value so that it will be dynamic for both the values    
      $nextDiv.find('select.appr_type,input.inpt_specific_user')
        .toggle(); // toggle select/input =>hide/show
    
    }
    $('#approval_approvers_attributes_0_approver_type').on('change', function() {
    
      $.fn.myFunc(this);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="field">
      <label for="approval_approvers_attributes_0_approver_type">Approver</label>
      <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_type" name="approval[approvers_attributes][0][approver_type]" onchange="">
        <option selected="selected" value="Role">Role</option>
        <option value="Specific User">Specific User</option>
      </select>
      </br>
      </br>
    </div>
    <div class="field" class="role">
      <label for="approval_approvers_attributes_0_approver_value">Role</label>
      <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]">
        <option value="hr manager">hr manager</option>
        <option selected="selected" value="reporting manager">reporting manager</option>
      </select>
      <input type="text" class="inpt_specific_user" style="display:none" />
      <input id="approval_approvers_attributes_0__destroy" name="approval[approvers_attributes][0][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> 
      </br>
      </br>
    </div>

    更新,代码不使用额外元素并使用replaceWith()

    $.fn.myFunc = function(button) {
      var id = $(button).attr('id');
      id = button.id;
      var $nextDiv = $(button).closest('div.field') // get closest parent field
        .next('div.field') // get next parent field
      $nextDiv.find('label') // find label
        .text(button.value); //change text, this.value so that it will be dynamic for both the values
      var $el = null;
      if (button.value != 'Role') {
        $nextDiv.find('select.appr_type').replaceWith('<input type="text" class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]"  />');
      } else {
        $nextDiv.find('input.appr_type').replaceWith('<select class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]">\
            <option value="hr manager">hr manager</option>\
            <option selected="selected" value="reporting manager">reporting manager</option>\
        </select>');
      }
    }
    $('#approval_approvers_attributes_0_approver_type').on('change', function() {
      $.fn.myFunc(this);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="field">
      <label for="approval_approvers_attributes_0_approver_type">Approver</label>
      <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_type" name="approval[approvers_attributes][0][approver_type]" onchange="">
        <option selected="selected" value="Role">Role</option>
        <option value="Specific User">Specific User</option>
      </select>
      </br>
      </br>
    </div>
    <div class="field" class="role">
      <label for="approval_approvers_attributes_0_approver_value">Role</label>
      <select class="form-control appr_type" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]">
        <option value="hr manager">hr manager</option>
        <option selected="selected" value="reporting manager">reporting manager</option>
      </select>
    
      <input id="approval_approvers_attributes_0__destroy" name="approval[approvers_attributes][0][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> 
      </br>
      </br>
    </div>

    【讨论】:

    • 感谢您的即时回复。当用户选择“特定用户”时,如何将下拉列表更改为文本框。在再次选择特定用户后,如果他在这种情况下将选择更改为角色,如何再次将此“文本框”更改为“下拉菜单”?
    • 该功能不工作。标签文本没有改变。
    • 但我不能使用另一个输入字段,为什么因为这个 html 是由 rails 中的嵌套表单 gem 生成的。我不能上课,我一开始也不能隐藏它。即使我给所有这些字段将包含相同的类。我的意思是这是一个批准者条目的标记。像这样,可能有任意数量的批准者在场。如果有 10 个批准者,此标记将重复 10 次。所以我想将下拉菜单更改为文本框
    • 很抱歉,它被意外点击了。
    • 检查我的更新答案并仅供参考...再次单击相同的答案将撤消否决票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    相关资源
    最近更新 更多