传递js变量以形成动作url

编程入门 行业动态 更新时间:2024-10-26 00:27:38
本文介绍了传递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

本文发布于:2023-10-12 01:31:43,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   动作   js   url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!