2015-01-04 53 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重定向发送请求。

相关问题