没有JS可以将数据放入action属性吗?

Is it possible to put data into the action atribute without 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>

是否可以提交表单,使请求的 URI 取决于用户在 input 中键入的内容?我的意思是,没有明确地写 JavaScript,只通过 HTML.

简答:不,这不可能。

使用 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> 

不,如果没有 JavaScript

,您将无法做到这一点

使用普通内联 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>

您无法使用 HTML 更改 action 属性的值,但您 可以 使请求 URL (URI) 取决于在用户输入上。 (不清楚你问的是哪一个,但我想你是想问后者。)事实上,当你使用 method=get (默认值),当你分配一个 name 属性时,这会自动发生到表单中的 input 元素:

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

如果此表单出现在通过 HTTP 访问的域 www.example.com 中的页面上,并且如果用户输入为 42,则将生成请求 URL http://www.example.com/employee/?emp_id=42。然后在 www.example.com 的服务器上从那里获取它。

如果 URL 中没有以 ? 开头的查询部分,则无法执行此操作。如果您需要生成特定格式的请求 URL,请说 http://www.example.com/employee/42,其中 42 是用户输入,如果您不能或不想使用 JavaScript,您需要一个中继服务,它接受查询 URL 作为输入并以一种相当简单的方式将其转换为所需的格式并通过 HTTP 重定向发送请求。