2009-10-22 35 views
0

在修改之後我有下面的代碼在HTML如何調用的innerHTML在Firefox

<div id="divTest"> 
     Hello 
     <input type="text" id="txtID"/>  <input type="text" id="txtID"/>   
    </div> 
    <input type="button" value="Click" /> 

,我想modifiy innerHTML來得到我的在Firefox運行形式的所有控件的值。

請參閱下面的javascript代碼:

<script type="text/javascript"> 

(function($) 
{ 
    var oldHTML = $.fn.html; 

    $.fn.formhtml = function() { 
    if (arguments.length) return oldHTML.apply(this,arguments); 
    $("input,textarea,button", this).each(function() { 
     this.setAttribute('value',this.value); 
    }); 
    $(":radio,:checkbox", this).each(function() { 
     // im not really even sure you need to do this for "checked" 
     // but what the heck, better safe than sorry 
     if (this.checked) this.setAttribute('checked', 'checked'); 
     else this.removeAttribute('checked'); 
    }); 
    $("option", this).each(function() { 
     // also not sure, but, better safe... 
     if (this.selected) this.setAttribute('selected', 'selected'); 
     else this.removeAttribute('selected'); 
    }); 
    return oldHTML.apply(this); 
    }; 

    //optional to override real .html() if you want 
    $.fn.html = $.fn.formhtml; 
}); 
$(document).ready(function() 
{ 
    $('input[type=button]').click(function() { 
     alert($('#divTest').html()); 
    }); 
}); 
</script> 

上述功能不是爲我工作。我想調用innerHtml的div ID 「divTest」 後,它的控制值被分配給它。請有看看到上面的代碼,讓我知道我需要修改上面的代碼

+0

不知道你正在嘗試做的。你可以總結一句話,你在做什麼(忽略所有的代碼)。例如「我想換一個元素的.innerHTML當我點擊一個按鈕」 – scunliffe 2009-10-22 16:02:28

回答

0

我用下面的jQuery

<script type="text/javascript"> 



$(document).ready(function() 



{ 

    var oldHTML = $.fn.html; 



    $.fn.formhtml = function() { 

    if (arguments.length) return oldHTML.apply(this,arguments); 

    $("input,textarea,button", this).each(function() { 

     this.setAttribute('value',this.value); 

    }); 

    $(":radio,:checkbox", this).each(function() 

    { 



     if (this.checked) this.setAttribute('checked', 'checked'); 

     else this.removeAttribute('checked'); 

    }); 

    $("option", this).each(function() 

    {  

     if (this.selected) this.setAttribute('selected', 'selected'); 

     else this.removeAttribute('selected'); 

    }); 

    return oldHTML.apply(this); 

    }; 

    $.fn.html = $.fn.formhtml; 

}); 

$(document).ready(function() 

{ 

    $('input[type=button]').click(function() { 

     alert($('#divTest').html()); 

    }); 

}); 

    </script> 

解決我的問題和HTML是如下:

<div id="divTest"> 

    Hello 

    <input type="text" id="txtID" /> 

    <input type="text" id="txtID" /> 

    <input type="text" /> 

    <input type="text" id="Text1" /> 

    <input type="text" id="Text1" /> 

    <input type="text" /> 

    <input type="text" /> 

    <input type="text" /> 

    <input type="text" id="Text5" /> 

    <input type="text" id="Text6" /> 

</div> 

<input type="button" value="Click" /> 
0

沒有被執行的第一個匿名函數...是什麼呢?

如果要執行它

,添加兩個括號像

})(); 

對不起結束時,如果它不具有anythig做的,但我不明白它

0

???首先,如果您想讓jquery/javascript查找或操作元素或其內容,則不能有兩個具有相同ID名稱的html元素。你是否想要獲得Form元素值?抓取innerHTML塊不會幫助您讀取值。