2010-09-22 28 views
0

我有一個具有不同控件的網頁。我必須通過按鈕單擊某個特定的div元素的值來獲取html源代碼。使用jQuery獲取填充表單的HTML代碼

jQuery .html()方法給了我沒有值的控件的html源碼,但我需要html源碼和用戶選擇的值。

回答

4

嘗試遍歷每個<input>並在訪問html之前將其設置爲value屬性爲其DOM值。

的jsfiddlehttp://jsfiddle.net/AWK9S/3/

$('form input').each(function() 
{ 
    this.setAttribute('value',this.value); 
    if (this.checked) 
     this.setAttribute('checked', 'checked'); 
    else 
     this.removeAttribute('checked'); 
}); 

$('form select').each(function() 
{ 
    var index = this.selectedIndex; 
    var i = 0; 
    $(this).children('option').each(function() 
    { 
     if (i++ != index) 
      this.removeAttribute('selected'); 
     else 
      this.setAttribute('selected','selected'); 
    }); 
}); 

$('form textarea').each(function() 
{ 
    $(this).html($(this).val()); 
}); 

的jsfiddlehttp://jsfiddle.net/AWK9S/3/

+0

我會用'$( ':輸入')',而不是爲'input'不匹配'''和'