Adding Hidden Value On UL and LI Tags, How?

做~自己de王妃 提交于 2020-01-06 20:54:14

问题


I have multiple options like this :

<li>
<a href="#" title="" class="selected"><span class="to-admin">Administrator</span></a>
<input id="shareto" type="hidden" value="0-1" name="shareto">
</li>
<li>
<a href="#" title=""><span class="to-finance">Finance</span></a>
<input id="shareto" type="hidden" value="1-1" name="shareto">
</li>
<li>
<a href="#" title=""><span class="to-technician">Technician</span></a>
<input id="shareto" type="hidden" value="1-0" name="shareto">
</li>
<li>
<a href="#" title=""><span class="to-lawyer">Legal</span></a>
<input id="shareto" type="hidden" value="0-0" name="shareto">
</li>

and now I want to 'catch' that variable using this PHP :

$Type = $_POST['shareto'];

why I always get the last value, no matter what option I choose? how to do it correctly? thanks before.


回答1:


Please check this working fiddle link http://jsfiddle.net/7GHug/

I think you want something like this.

<ul>
<li data-val="0-1">
<a href="#" title="" class="selected"><span class="to-admin">Administrator</span></a>

</li>
<li data-val="1-1">
<a href="#" title=""><span class="to-finance">Finance</span></a>

</li>
<li data-val="1-0">
<a href="#" title=""><span class="to-technician">Technician</span></a>
</li>
<li data-val="0-0">
<a href="#" title=""><span class="to-lawyer">Legal</span></a>
<input id="shareto" type="hidden" value="" name="shareto">
</li>
</ul>​

Javascritp

$(document).ready(function($) {
        $('ul li').on('click', function() {
            $('input#shareto').val($(this).data('val'));
        });
    });

With single hidden input you can update the value based on the clicked li. ​




回答2:


If you change name=shareto to name=shareto[] then $_POST['shareto']; will be an array containing all the values.

Also element ids should be unique.




回答3:


Add [] like name="shareto[]" then $Type = implode(',',$_POST['shareto']);


来源:https://stackoverflow.com/questions/11675032/adding-hidden-value-on-ul-and-li-tags-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!