简体   繁体   中英

How to get li tag value by javascript or jquery

I need to get li value onclick function in php ...and want to store that in php session.. i used my code for this but this is not working.... i need to know that how to get li value on click function in java script.. i NEED to get li tag value of on click function and want to store in php session by js or jquery

<li class="atleta" value="100" >
            <a href="#"  >Vendor Registration</a>
</li>

$('.atleta').click(function(e) {


            var <?php  $_SESSION['VENDR']?>  = $(this));

});

Don't mix server side and client side technologies .Instead to php session i will recommend to use localStorage instead and store clicked li value in it as shown :-

$('.atleta').click(function(e) {
   localStorage.setItem('lival',$(this).val());
});

and in order to get data stored in localStorage use this :-

var livalue = localStorage.getItem('lival');

Reference

Or Instead of localStorage you can use sessionStorage also(their basic usage is same except for some difference refer here )

try this -

$('.atleta').click(function(e) {

        e.preventDefault(); //if needed
        var <?php  $_SESSION['VENDR']?>  = $(this).children('a').text();

});

i used this and its working..

<li id="atleta" value="18" onclick="vendorData();">
            <a href="#" >Vendor Registration</a>
</li>

<script>

function vendorData(){
    var h = document.getElementById('atleta').value;
    localStorage.setItem("vendor", h);
    var ven = localStorage.getItem("vendor");

    }
</script>

您可以将变量存储在Cookie中,因此可以通过jQuery进行设置,然后再获取php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM