2015-01-04 45 views
0

我有以下形式:是否可以將數據放入沒有JS的操作屬性中?

<form action="/employee/_here_should_be_the_value_of_the_emp_id_input" method="get"> 
     <label for="employeeId">Id:</label> 
     <input type="text" id="emp_id"/> 
     <input type="submit" /> 
</form> 

是否可以提交表單使得requsted URI將取決於什麼樣的用戶類型爲input?我的意思是,沒有明確寫入JavaScript,只能通過HTML

+0

你可以使用服務器端語言嗎? – 2015-01-04 11:19:06

回答

3

簡答:不,不可能。

使用jQuery它可以是這樣的:

<script> 
$(function() { 
    var input = $('#emp_id'); 
    input.change(function() { 
    $('#form').attr('action', '/employee/' + input.val()); 
    }); 
}); 
</script> 

<form id="form" action="/employee/_here_should_be_the_value_of_the_emp_id_input" method="get"> 
     <label for="employeeId">Id:</label> 
     <input type="text" id="emp_id"/> 
     <input type="submit" /> 
</form> 
+0

也許你提出一個快速的解決方案來做到這一點與jQuery? – user3663882 2015-01-04 11:19:12

+0

這就是一個不同的問題 – Sarath 2015-01-04 11:20:32

1

不,你不能這樣做,沒有JavaScript

+0

我怎樣才能做到這一點與jQuery? – user3663882 2015-01-04 11:19:46

+0

在那裏你沒有足夠的代碼片段和最終目標的細節來告訴你應該怎麼做 – 2015-01-04 11:22:37

0

隨着普通直列JS:

<form id="form" oninput="document.getElementById('form').action = '/employee/' + document.getElementById('emp_id').value" action="/employee/_here_should_be_the_value_of_the_emp_id_input" method="get"> 
     <label for="employeeId">Id:</label> 
     <input type="text" id="emp_id"/> 
     <input type="submit" /> 
</form> 
+0

問題說「沒有JS」。 – 2015-01-04 12:18:59

+0

@ JukkaK.Korpela我知道,但問題被標記爲Javascript和最終他要求一個JS解決方案。 – NatureShade 2015-01-04 13:54:16

1

你不能改變的值action屬性與HTML,但你可以使請求URL(URI)取決於用戶輸入。 (目前還不清楚你問的是哪一個,但我認爲你打算詢問後者。)實際上,當你使用method=get(默認值)時,會自動發生這種情況,當你在表單中爲input元素分配name屬性時:

<form action="/employee/" method="get"> 
     <label for="emp_id">Id:</label> 
     <input type="text" id="emp_id" name="emp_id"/> 
     <input type="submit" /> 
</form> 

如果在網頁上顯示在域www.example.com與HTTP訪問這種形式,並且如果用戶輸入是42,這將生成請求URL http://www.example.com/employee/?emp_id=42。然後從www.example.com上的服務器上取下它。

如果在URL中沒有查詢部分,則無法執行此操作,從?開始。如果您需要生成特定格式的請求網址,請說http://www.example.com/employee/42,其中42是用戶輸入,如果您不能或不想使用JavaScript,則需要一個接受查詢URL作爲輸入和轉換的中繼服務一個相當平凡的方式,它到所需的格式並通過HTTP重定向發送請求。

相關問題