传递js变量以形成动作url [英] Pass js variable to form action url

查看:73
本文介绍了传递js变量以形成动作url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行,或者我是否以错误的方式进行操作?

我有一个表单提交时,应该发送给用户到一个URL取决于输入。



例如,如果用户输入'2',则URL应该是 books / itemView?id = 2



我创建了一个 var = id ,它接受搜索输入框的数据。是否有可能将此变量添加到我当前的表单操作?也许有更有效的方法吗?



我现在的代码如下;

 < form id =searchaction =<?php echo URL;?> books / itemView?id =method =post> 
< input type =textname =searchid =demo/>
< input type =submitvalue =提交>
< / form>

我的JS

 < code $ $(document).ready(function(){

var id = $('#search')。val();

});

对于JS来说很新,所以对它有所帮助。

JS应该是

  $('#search')。on 'submit',function(){
var id = $('#demo')。val();
var formAction = $('#search')。attr('action');
$('#search')。attr('action',formAction + id);
});


I'm not sure if this is possible, or if I am doing it the wrong way?

I have a form that when submitted, should send the user to a URL depending on the input.

e.g If the users inputs '2', the URL should be books/itemView?id=2

I have created a var = id, which takes the search input box data. Is it possible to add this variable to my current form action? Perhaps there is a more efficient way?

My current code is as follows;

<form id="search" action="<?php echo URL; ?>books/itemView?id=" method="post">
<input type="text" name="search" id="demo"/>
<input type="submit" value="Submit">
</form>

My JS

$(document).ready(function(){

var id = $('#search').val();

});

Quite new to JS so any help appreciated.

解决方案

JS should be

$('#search').on('submit', function() {
    var id = $('#demo').val();
    var formAction = $('#search').attr('action');
    $('#search').attr('action', formAction + id);
});

这篇关于传递js变量以形成动作url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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