1. 程式人生 > >獲得元素html內容 及 表單元素狀態(即使用者輸入或者選中的內容)

獲得元素html內容 及 表單元素狀態(即使用者輸入或者選中的內容)

   //獲得元素html內容.如果process為true時,獲得html及表單元素狀態(即使用者輸入或者選中的內容)
   function getHtml(selecter,process) {
       var ele = $(selecter);
       if (window.ActiveXObject || !process) return ele.html();
       else {
           ele.find('input,select,textarea').each(function () {
               switch (this.tagName) {
                   case 'INPUT':
                   case 'TEXTAREA': 
                       switch (this.type) {
                           case 'radio':
                           case 'checkbox':
                               if (this.checked) this.setAttribute('checked', true);
                               else this.removeAttribute('checked');
                               break;
                           default:
                               this.setAttribute('value', this.value);
                       }
                       break;
                   case 'SELECT':
                       // $('option', this).removeAttr('selected').not(':selected').removeAttr('selected').end().filter(':selected').attr('selected', 'selected');
                       $(this).find('option').filter(':selected').attr('selected', 'selected').siblings().removeAttr('selected');
                       break;
               }
           });
           return ele.html();
       }
   }