2013-11-25 81 views
0

給定輸入元件...跨瀏覽器替代document.body.innerHTML

<input type="text" /> 

和此JavaScript ...

var x = document.body.innerHTML; 

用戶輸入的值,說 「myvalue的」,和原因x至被設置

IE將設置X =

<input type="text" value="myValue" /> 

雖然Chrome會設置x =

<input type="text" /> 

那麼,是否有任何跨瀏覽器替代document.body.innerHTML?

+0

[Cross domain iframe issue]的可能重複(http://stackoverflow.com/questions/9393532/cross-domain-iframe-issue) – DevlshOne

+1

Chrome是最新版本。 與此類似,但與跨域iframe問題有所不同。 代碼包含在按下按鈕時調用的函數中,即在頁面完全加載後調用。 – cymorg

+1

[Inner HTML with input values]的可能重複(http://stackoverflow.com/questions/12126497/inner-html-with-input-values) –

回答

0

這看起來像跨作品瀏覽器。 JavaScript函數做到這一點...

  • 設定值/選擇/ HTML每個表單輸入的屬性與用戶
  • 輸入值/選擇/ textarea的創建了一個名爲「formContent」
  • 新的輸入元素設置「formContent」的值(由用戶輸入含值)的形式的元素的編碼值
  • 添加「formContent」元素的形式
  • 提交包括由用戶輸入到
  • 值的形式

這是我的示例源...

<html> 
<head> 
<script src="jquery-1.2.6.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    function submitFormWithValues() { 
     $('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()); 
     }); 

     alert(encodeURI($("form").html())); 

     var myInput = document.createElement("input"); 
     myInput.type = "text"; 
     myInput.setAttribute("name", "myInput"); 
     myInput.setAttribute("value",encodeURI($("form").html())); 

     var myForm = document.getElementById("form1"); 
     myForm.appendChild(myInput); 
     myForm.submit;    
    } 
</script> 
</head> 
<body id="body"> 
<form id="form1" action="test.aspx" method="post"> 
<input type="text" id="input1" /><br /> 
<input type="text" id="input2" /><br /> 
<input type="submit" onclick="this.style.visibility='hidden', submitFormWithValues()" id="submit" value=" Submit " /> 
</form></body></html> 

當頁面提交的表單將類似於此...

<form id="form1" action="test.aspx" method="post"> 
<input type="text" id="input1" /><br /> 
<input type="text" id="input2" /><br /> 
<input type="text" name="formContent" value=" %3CINPUT%20id=input1%20value=%22first%20value%22%20jQuery1385425785747=%222%22%3E%3CBR%3E%3CINPUT%20id=input2%20value=%22second%20value%22%20jQuery1385425785747=%223%22%3E%3CBR%3E%3CINPUT%20style=%22VISIBILITY:%20hidden%22%20id=submit%20onclick=%22this.style.visibility='hidden',%20submitFormContent()%22%20value=%22%20Submit%20%22%20type=submit%20jQuery1385425785747=%224%22%3E%20" /> 
</form> 

接收頁(C#下面的例子)重建HTML通過解碼formContent的值來形成元素。

using System; 

public partial class test : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.Write(Server.UrlDecode(Request.Form.Get("formcontent"))); 
    } 
} 

接收頁面將輸出原始表單元素,包括用戶輸入的值。通過這種方式,輸入的表單和任何值可以從一個頁面傳遞到另一個頁面,而與瀏覽器功能無關。