change type of input field with jQuery

后端 未结 29 2107
青春惊慌失措
青春惊慌失措 2020-11-22 05:13
$(document).ready(function() {
    // #login-box password field
    $(\'#password\').attr(\'type\', \'text\');
    $(\'#passwo         


        
29条回答
  •  抹茶落季
    2020-11-22 05:34

    I like this way, to change the type of an input element: old_input.clone().... Here is an example. There is an check box "id_select_multiple". If this is is changed to "selected", input elements with name "foo" should be changed to check boxes. If it gets unchecked, they should be become radio buttons again.

      $(function() {
        $("#id_select_multiple").change(function() {
         var new_type='';
         if ($(this).is(":checked")){ // .val() is always "on"
              new_type='checkbox';
         } else {
             new_type="radio";
         }
         $('input[name="foo"]').each(function(index){
             var new_input = $(this).clone();
             new_input.attr("type", new_type);
             new_input.insertBefore($(this));
             $(this).remove();
         });
        }
      )});
    

提交回复
热议问题