2011-04-11 16 views
0

如何獲取查詢字符串的值並將其置於輸入框中?目前我有:從查詢字符串不工作的位置值到輸入框

<input type="text" name="spouse" id="spouse" value="<script type="text/javascript"> 
     document.write("Name: " + Request.QueryString("spouse")); 

    </script>"/> 

但是,只需要腳本和其所有內容並將其放入輸入框中。

我希望能夠採取從該代碼來我查詢字符串:

<tr > 
<td><input type="text" name="n1" value="Duck, Donald" /></td> 
<td><input type="text" name="n2" value="Daisy" /></td> 
<td><input type="button" value="Show" title="Show" 
      onclick="location.href='example123.html?name=' + escape(this.form.n1.value)+ '&spouse=' + escape(this.form.n2.value);" /> 

</td> 

,並具有價值的名稱或配偶出現一個輸入框裏面。從查詢字符串中將值放入輸入框的正確方法是什麼?

+0

第一個例子甚至無法運行(可能在IE上),因爲有未轉義的引號。 – Blender 2011-04-11 21:25:19

+0

我會使用onLoad或類似的東西。如果您使用onClick,則用戶每次點擊該控件時都會丟失數據。 – 2011-04-11 21:27:11

+0

這是ASP.NET代碼,並且您使用的是任何JavaScript框架(這些工作中的某些可能是抽象的,以幫助您節省一些頭痛)? – jcolebrand 2011-04-11 21:27:46

回答

0

Request.QueryString不是原生JavaScript函數。使用document.location對象並解析出所需的值。

+0

讓我覺得他在做ASP.NET ...... – jcolebrand 2011-04-11 21:26:57

+0

你如何使用document.location解析出一個值? – will 2011-04-11 21:31:00

0

也許使用onload函數來執行您需要的操作。一旦文檔已經完全加載,這會調用你的函數,所以你知道html中的所有標籤都將存在,並且可以被正確地引用。

例如。

<html> 
    <head> 
     <title>Value Setting</title> 
     <script type="text/javascript"> 
      window.onload = (function() { 
       document.getElementById('spouse').value = "one way"; 
       document.forms[0].elements[1].value = "another way"; 
       /* note elements refers to only input children */ 
      }); 
     </script> 
    </head> 
    <body> 
    <form id="myform"> 
     <div> 
      <input id="first-field" value="first-field" onchange="this.value += ' an example';"/> 
     </div> 
     <div> 
      <p> 
       <input id="spouse" value="spouse"/> 
      </p> 
     </div> 
    </form> 
    </body> 
</html> 

您不能在屬性中嵌入元素,因爲這不是有效的html。雖然有些屬性可以評估爲JavaScript。即屬性,如動作,onchange,onclick等。

+0

如何從查詢字符串中檢索值以便在配偶來自發送頁面的情況下使用它? – will 2011-04-12 00:11:31