我有一个包含行的表。每行都有自己的属性,称为data
。我想将所有tr放入隐藏的输入类型。我怎样才能做到这一点?
我想在服务器端获得$('#valuePermission').val()
。
<form >
<input type="hidden" name="valuePermission[]" id="valuePermission" value=""/>
<table class='table table-hover table-striped'>
<tr data="1+a"><td>name:1</td><td>type:a</td></tr>
<tr data="2+b"><td>name:2</td><td>type:b</td></tr>
<tr data="3+c"><td>name:3</td><td>type:c</td></tr>
</table>
<button type="submit" id="btn">submit</button>
</form>
<script>
$('#btn').on('click',function(e){
e.preventDefault();
var dt=[];
$('table').find('tr').each(function(){
dt.push($(this).attr('data'));
});
$('#valuePermission').val(dt);
});
$('form').submit();
</script>
在服务器端我想要这样:
<?php $var=$_POST['valuePermission'];
if(isset($var && !empty($var))
$var=json_decode($var);
foreach($var as $v=>$k)
echo $v;
?>
结果是这样的:
*5+sport+500001+soccer,*5+sport+500002+vollyball,*5+sport+500003+swimming,*5+sport+500004+running,*5+sport+500006+tenis,*5+sport+500007+table tenis
我想要像这样:
array[0]='5+sport+500001+soccer'
array[1]='5+sport+500001+vollyball
和....
答案 0 :(得分:3)
将其作为json字符串传递 JSON.stringify()
可用于此,然后从服务器端解码并使用。同时阻止默认点击事件操作(此处是表单提交),否则在隐藏字段值更新之前提交表单,使用 event.preventDefault()
。最后使用代码提交表单。
<form >
<input type="hidden" name="valuePermission" id="valuePermission" value=""/>
<table class='table table-hover table-striped'>
<tr data="1+a"><td>name:1</td><td>type:a</td></tr>
<tr data="2+b"><td>name:2</td><td>type:b</td></tr>
<tr data="3+c"><td>name:3</td><td>type:c</td></tr>
</table>
<button type="submit" id="btn">submit</button>
</form>
<script>
$('#btn').on('click',function(e){
e.preventDefault();
var dt=[];
$('table').find('tr').each(function(){
dt.push($(this).attr('data'));
});
$('#valuePermission').val(JSON.stringify(dt));
$(this).closest('form').submit();
});
</script>
服务器端使用 json_decode()
解码json字符串
<?php $var = json_decode($_POST['valuePermission']);
foreach($var as $v=>$k)
echo $v;
?>
更新:
您可以尝试的另一种方法是创建多个隐藏输入
<form>
<input type="hidden" name="valuePermission[]" value="" />
<input type="hidden" name="valuePermission[]" value="" />
<input type="hidden" name="valuePermission[]" value="" />
<table class='table table-hover table-striped'>
<tr data="1+a">
<td>name:1</td>
<td>type:a</td>
</tr>
<tr data="2+b">
<td>name:2</td>
<td>type:b</td>
</tr>
<tr data="3+c">
<td>name:3</td>
<td>type:c</td>
</tr>
</table>
<button type="button" id="btn">submit</button>
</form>
<script>
$('#btn').on('click', function(e) {
e.preventDefault();
var $input = $('name="valuePermission[]"');
return $('table').find('tr').each(function(i) {
$input.eq(i).val($(this).attr('data'));
})
$(this).closest('form').submit();
});
</script>
答案 1 :(得分:0)
您只需使用输入名称valuePermission
代替valuePermission[]
,也可以使用表单操作和方法
<form id="myform" action="target.php" method="post" >
<input type="hidden" name="valuePermission" id="valuePermission" value=""/>
<table class='table table-hover table-striped'>
<tr data="1+a"><td>name:1</td><td>type:a</td></tr>
<tr data="2+b"><td>name:2</td><td>type:b</td></tr>
<tr data="3+c"><td>name:3</td><td>type:c</td></tr>
</table>
<button type="submit" id="btn">submit</button>
</form>
<script>
$(document).ready(function(){
$('#btn').click(function(){
var dt='';
$('.table').find('tr').each(function(){
if(dt == '')
{
dt = $(this).attr('data');
}
else
{
dt+='*'+$(this).attr('data');
}
});
if($('#valuePermission').val(dt))
{
$( "#myform").submit();
}
});
});
</script>
在目标文件
中<?php
if(isset($_POST['valuePermission']))
{
$var = explode('*',$_POST['valuePermission']);
foreach($var as $k => $v)
{
echo $v;
echo "<br>";
}
}
?>
已修改为爆炸
$str = '*5+sport+500001+soccer,*5+sport+500002+vollyball,*5+sport+500003+swimming,*5+sport+500004+running,*5+sport+500006+tenis,*5+sport+500007+table tenis';
$str = explode('*',$str);
$newArray =array();
foreach(array_filter($str) as $val)
{
$newArray[] = trim($val,',');
}
echo "<pre>"; print_r($newArray);
这会给你:
Array
(
[0] => 5+sport+500001+soccer
[1] => 5+sport+500002+vollyball
[2] => 5+sport+500003+swimming
[3] => 5+sport+500004+running
[4] => 5+sport+500006+tenis
[5] => 5+sport+500007+table tenis
)