Javascript 如何将动态附加值发送到控制器
这是我的控制器页面调用:Javascript 如何将动态附加值发送到控制器,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,这是我的控制器页面调用: $("#GroupID").append( group ); //in here Example: group value=2 我想在Ajax数据:或input type=hidden表单中添加该组id。。我该怎么做 $("#GdStartTest").click(function(){ $.ajax({ data: {Gd: $("#participantsForGd").serialize(), 'action': 'startGdstudents', }
$("#GroupID").append( group ); //in here Example: group value=2
我想在Ajax数据:
或input type=hidden
表单中添加该组id。。我该怎么做
$("#GdStartTest").click(function(){
$.ajax({
data: {Gd: $("#participantsForGd").serialize(), 'action': 'startGdstudents', },
“.text()”:将返回元素的内容
如果要创建隐藏输入,可以按以下方式执行:
$("#GdStartTest").click(function(){
$.ajax({
data: {Gd: "sometext-"+ $("#GroupID").text(), 'action': 'startGdstudents', }
像这样试试
$("#groupID").val(); // this will return you the value
$("#GdStartTest").click(function(){
$.ajax({
data: {Gd: "sometext-"+ $("#GroupID").val(), 'action': 'startGdstudents', }
$("#GdStartTest").click(function(){
data = $("#participantsForGd").serialize();
data.action = 'startGdstudents';
$.ajax({
type: 'POST',
data: data,
url:
.......
.......
});
});
为提交
$("#GroupID").val(group);
在php文件中,您可以获取如下值
$("#groupID").val(); // this will return you the value
$("#GdStartTest").click(function(){
$.ajax({
data: {Gd: "sometext-"+ $("#GroupID").val(), 'action': 'startGdstudents', }
$("#GdStartTest").click(function(){
data = $("#participantsForGd").serialize();
data.action = 'startGdstudents';
$.ajax({
type: 'POST',
data: data,
url:
.......
.......
});
});
在表单中也放入一个隐藏的输入,它将被包含在
serialize()
//下面是组id示例:2
启动测试
data:{Gd:$(“#GroupID”).text()..它正在使用中,但在数据中它不会使用,你能建议如何处理变量吗?var GroupID=$(“#GroupID”).text();
我试过这个|:数据:{participantsForGd:$(“#participantsForGd”).serialize(),'action':'startGdstudents','GroupID},
检查答案,我用一些文本对其进行了压缩。如果您需要获取另一个字段,则每次都要调用val()。因此,使用serialize是处理表单的最佳方式。因此,尝试隐藏属性并设置值而不是文本将是最佳方式。显示我编辑的代码..可能是您的最佳方式…如果您想这样做..serialize()
单击提交按钮后的表单值。value=“2”是动态的。如果您需要获取另一个字段,则每次都要调用val()
。因此,使用序列化是表单的最佳方式。
$id = $_POST['GroupID'];
$action = $_POST['action'];
<form id="participantsForGd">
<input type="hidden" value="2" name="GroupID">
<span id="GroupID"></span> //in here group id is coming example: 2
<input type="submit" id="GdStartTest">Start test</button>
</form>