jquery / Javascript根据输入字段更改表单动作 [英] Jquery / Javascript change form action based on input field

查看:106
本文介绍了jquery / Javascript根据输入字段更改表单动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表格

 < html> 
< body>
< form action =''name ='myform'method ='POST'>
< input type ='text'name ='cars'>
< button action ='submit'>搜索汽车< / button>
< / body>
< / html>

我想要做的是将表单动作改为类似于 action ='http://www.mysite.com/< cars_value> .html'> 根据填入输入字段的内容(由自动填充填充)。



有没有简单的方法来做到这一点?我可以很轻松地用< select> 来完成,但客户需要一个输入字段!

解决方案

你可以做到这一点,你需要添加ID到物品。您可能想要做的一件事是验证用户输入。



HTML

 < form action =''name ='myform'id =myformmethod ='POST'> 
< input type ='text'name ='cars'id =cars>
< button action ='submit'>搜索汽车< / button>
< / form>

jQuery
< $($#$)$('#c'c)$('#myform')。submit(function(){
var car = $('#cars')。val();
$这个).attr('action',http://www.mysite.com/+ car +.html);
});

编辑:
最好是页面底部的javascript;


$ b $ p FULL

 <!DOCTYPE HTML> 
< html lang =en-US>
< head>
< meta charset =UTF-8>
< title>< / title>
< / head>
< body>
< form action =''name ='myform'id =myformmethod ='POST'>
< input type ='text'name ='cars'id =cars>
< button action ='submit'>搜索汽车< / button>
< / form>
< script src =path-to-jquery>< / script>
< script>
//速记$(document).ready();
$(function(){
$('#myform')。submit(function(){
var car = $('#cars')。val();
$(this).attr('action',http://www.mysite.com/+ car +.html);
});
});
< / script>
< / body>
< / html>


I have a form like this

<html>
    <body>
    <form action='' name='myform' method='POST'>
    <input type='text' name='cars'>
    <button action='submit'>Search Cars</button>
    </body>
</html>

What I want to do is change the form action to be something like action='http://www.mysite.com/<cars_value>.html'> based on whatever is filled into the input field (which is populated by autocomplete).

Is there an easy way of doing this? I can do it with a <select> easily enough but the customer wants an input field instead!

解决方案

You can do this, you will need to add ids to items. One thing you might want to do is validate the users input.

HTML

<form action='' name='myform' id="myform" method='POST'>
  <input type='text' name='cars' id="cars">
  <button action='submit'>Search Cars</button>
</form>

jQuery

$('#myform').submit(function(){
  var car = $('#cars').val();
  $(this).attr('action', "http://www.mysite.com/" + car + ".html");
}); 

EDIT: It is best to be javascript at bottom of page;

FULL

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <form action='' name='myform' id="myform" method='POST'>
    <input type='text' name='cars' id="cars">
    <button action='submit'>Search Cars</button>
  </form>
  <script src="path-to-jquery"></script>
  <script>
  // Shorthand for $(document).ready();
  $(function() {
   $('#myform').submit(function(){
     var car = $('#cars').val();
     $(this).attr('action', "http://www.mysite.com/" + car + ".html");
   });
  });
 </script>
</body>
</html>

这篇关于jquery / Javascript根据输入字段更改表单动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆