innerHTML with current form values

前端 未结 5 1728
太阳男子
太阳男子 2020-12-17 01:48

Need .innerHTML functionality but with the current form field values including input, select (selected option), and textarea.

So, given:

5条回答
  •  [愿得一人]
    2020-12-17 02:16

    Not completely satisfied with it, but this mostly works:

    $('input:text, input:hidden, input:password').each(function() {
      var v=this.value; 
      $(this).attr("magicmagic_value",v).removeAttr("value").val(v);
    });
    $('input:checkbox,input:radio').each(function() {
      var v=this.checked; 
      if(v) $(this).attr("magicmagic_checked","checked"); 
      $(this).removeAttr("checked"); 
      if(v) this.checked=true; 
    });
    $('select option').each(function() { 
      var v=this.selected; 
      if(v) $(this).attr("magicmagic_selected","selected"); 
      $(this).removeAttr("selected");
      if(v) this.selected=true; 
    });
    $('textarea').each(function() { 
      $(this).html(this.value); 
    });
    
    var magic=$('form').html().replace(/magicmagic_/g,"");
    
    $('[magicmagic_value]').removeAttr('magicmagic_value');
    $('[magicmagic_checked]').attr("checked","checked").
      removeAttr('magicmagic_checked');
    $('[magicmagic_selected]').attr("selected","selected").
      removeAttr('magicmagic_selected');
    
    alert(magic);
    

提交回复
热议问题