我想使用表单提交列表值,正确的方式是什么
我有一个动态生成的列表,这只是一个静态列表。那么,如何发送被单击的特定列表值呢?它可以通过为每个列表使用单独的名称来实现,但我不想这样做,因为我的列表是动态生成的,我必须从许多列表项中获取它的值。对于下拉列表中类似类型的问题,有一个解决方案。链接PHP:请求HTML表单下拉列表的值
<form action="" id="form-id" method="get">
<ol name="test">
<a href="#" onclick="document.getElementById('form-id').submit();">
<li>
<input type="hidden" value="testvalue"> //list value 1
Coffee
</li>
</a>
<a href="#" onclick="document.getElementById('form-id').submit();">
<li>
<input type="hidden" value="testvalue1"> //list value 2
Tea
</li>
</a>
</ol>
</form>
<?php
if(isset($_GET['test']))
echo $_GET['test'];
?>
我不能100%确定我是否理解您在这里的确切要求,但我认为这可能是您想要的?
<form action="" id="form-id" method="get">
<h1>Hidden values</h1>
<ol name="test">
<li><input type="hidden" value="testvalue">Cofee</li>
<li><input type="hidden" value="testvalue1">Tea</li>
</ol>
<input type='hidden' id='test' name='test' />
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['test'] ) ){
echo 'Test: '.$_GET['test'];
}
?>
</form>
<script>
window.onload=function(){
var form=document.getElementById('form-id');
var col=form.querySelectorAll('li');
for( var n in col ) if( col[n] && col[n].nodeType==1 ){
col[n].onclick=function(e){
document.getElementById('test').value=this.querySelectorAll('input[type="hidden"]')[0].value;
form.submit();
};
}
};
</script>