Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在按钮链接中使用表单输入_Javascript_Html - Fatal编程技术网

Javascript 在按钮链接中使用表单输入

Javascript 在按钮链接中使用表单输入,javascript,html,Javascript,Html,我试图基本上获取用户在用户名表单中输入的任何内容,然后在URL中使用它;如果输入为“hello”,则URL将以/hello结尾。我需要使用js来完成吗 <form class="form-horizontal" role="form"> <div class="form-group"> <label for="inputUsername" class="col-sm-2 control-label">Username</label> &

我试图基本上获取用户在用户名表单中输入的任何内容,然后在URL中使用它;如果输入为“hello”,则URL将以/hello结尾。我需要使用js来完成吗

<form class="form-horizontal" role="form">
  <div class="form-group">
  <label for="inputUsername" class="col-sm-2 control-label">Username</label>
  <div class="col-sm-5">
      <input type="text" class="form-control" id="inputUsername" placeholder="Username">
  </div>
  </div>

  <div class="o ">
  <div class="col-sm-offset-2 col-sm-10">
      <a class = "btn btn-default" href=inputUsername> Sign in / Sign up</a>
  </div>
  </div>
</form>

用户名

是的,您可以使用JavaScript获取用户名。如问题注释中所述,您可以更新表单,使其包含一个
onsubmit
属性,在该属性中,您可以实例化一个函数以获取名称

如果您希望使用inputUsername在Submit上定义重定向位置
onsubmit
,您可能需要使用jQuery的“preventDefault()”方法(已讨论)来插入标准操作表单提交

<form class="form-horizontal" role="form" onsubmit="onFormSubmit()">
  <div class="form-group">
  <label for="inputUsername" class="col-sm-2 control-label">Username</label>
  <div class="col-sm-5">
  <input type="text" class="form-control" id="inputUsername" placeholder="Username">
  </div>
 </div>

  <div class="o ">
  <div class="col-sm-offset-2 col-sm-10">
      <a class = "btn btn-default" href=inputUsername> Sign in / Sign up</a>
  </div>
  </div>
</form>

用户名
然后像这样做:

<script>
function onFormSubmit() {
    var inputUsername = document.getElementById("inputUsername").value;
    window.location = "/" + inputUsername;
}
</script>

函数onFormSubmit(){
var inputUsername=document.getElementById(“inputUsername”).value;
window.location=“/”+输入用户名;
}

在提交时更改url而不是输入。他的答案可能会有所帮助(查看评论):[[1]: