How to make a dropdown readonly using jquery?

后端 未结 21 1039
终归单人心
终归单人心 2020-12-02 06:54

I am using the following statement to make it readonly but it is not working.

$(\'#cf_1268591\').attr(\"readonly\", \"readonly\"); 

I don\

相关标签:
21条回答
  • 2020-12-02 07:31

    Simple jquery to remove not selected options.

    $('#your_dropdown_id option:not(:selected)').remove();
    
    0 讨论(0)
  • 2020-12-02 07:31

    This line makes selects with the readonly attribute read-only:

    $('select[readonly=readonly] option:not(:selected)').prop('disabled', true);
    
    0 讨论(0)
  • 2020-12-02 07:31

    It is an old article, but i want to warn people who will find it. Be careful with disabled attribute with got element by name. Strange but it seems not too work.

    this do not work:

    <script language="JavaScript">
    function onChangeFullpageCheckbox() {    
    $('name=img_size').attr("disabled",$("#fullpage").attr("checked"));
    </script>
    

    this work:

    <script language="JavaScript">
    function onChangeFullpageCheckbox() {    
    $('#img_size').attr("disabled",$("#fullpage").attr("checked"));
    </script>
    

    Yes, i know that i better should use prop and not attr, but at least now prop will not work because of old version of jquery, and now i cant update it, dont ask why... html difference is only added id: ...

    <select name="img_size" class="dropDown" id="img_size">
    <option value="200">200px
    </option><option value="300">300px
    </option><option value="400">400px
    </option><option value="500">500px
    </option><option value="600" selected="">600px
    </option><option value="800">800px
    </option><option value="900">900px
    </option><option value="1024">1024px
    </option></select>
    
    <input type="checkbox" name="fullpage" id="fullpage" onChange="onChangeFullpageCheckbox()" />
    

    ...

    I have not found any mistakes in the script, and in the version with name, there was no errors in console. But ofcourse it can be my mistake in code

    Seen on: Chrome 26 on Win 7 Pro

    Sorry for bad grammar.

    0 讨论(0)
  • 2020-12-02 07:32

    Try to make empty string when "keypress up"

    The Html is:

    <input id="cf_1268591" style="width:60px;line-height:16px;border:1px solid #ccc">
    

    The Jquery is:

    $("#cf_1268591").combobox({
      url:"your url",
      valueField:"id",
      textField:"text",
      panelWidth: "350",
      panelHeight: "200",
    });
    

    // make after keyup with empty string

    var tb = $("#cf_1268591").combobox("textbox");
      tb.bind("keyup",function(e){
      this.value = "";
    });
    
    0 讨论(0)
  • 2020-12-02 07:33

    Try this one.. without disabling the selected value..

    $('#cf_1268591 option:not(:selected)').prop('disabled', true);
    

    It works for me..

    0 讨论(0)
  • 2020-12-02 07:33

    As @Kanishka said , if we disable a form element it will not be submitted . I have created a snippet for this problem . When the select element is disabled it creates a hidden input field and store the value . When it is enabled it delete the created hidden input fields .

    More info

    jQuery(document).ready(function($) {
      var $dropDown = $('#my-select'),
        name = $dropDown.prop('name'),
        $form = $dropDown.parent('form');
    
      $dropDown.data('original-name', name); //store the name in the data attribute 
    
      $('#toggle').on('click', function(event) {
        if ($dropDown.is('.disabled')) {
          //enable it 
          $form.find('input[type="hidden"][name=' + name + ']').remove(); // remove the hidden fields if any
          $dropDown.removeClass('disabled') //remove disable class 
            .prop({
              name: name,
              disabled: false
            }); //restore the name and enable 
        } else {
          //disable it 
          var $hiddenInput = $('<input/>', {
            type: 'hidden',
            name: name,
            value: $dropDown.val()
          });
          $form.append($hiddenInput); //append the hidden field with same name and value from the dropdown field 
          $dropDown.addClass('disabled') //disable class
            .prop({
              'name': name + "_1",
              disabled: true
            }); //change name and disbale 
        }
      });
    });
    /*Optional*/
    
    select.disabled {
      color: graytext;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <form action="#" name="my-form">
      <select id="my-select" name="alpha">
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
      </select>
    </form>
    <br/>
    <button id="toggle">toggle enable/disable</button>

    0 讨论(0)
提交回复
热议问题