简体   繁体   中英

How to fill href tag with data from json and when clicked use the selected value in form to post

hope all of you have a really nice day. Im working on a project right now and i cant seem to get this right. I have the following Json.

{
"Error":200,
"Fecha":"6/19/2018",
"Nombreag":"Canchas de Tenis",
"Nombretges":"Reserva de cancha",
"horarios":[
    {
        "horaM":"1000",
        "hora":"10:00AM"
    },
    {
        "horaM":"1030",
        "hora":"10:30AM"
    },
    {
        "horaM":"1100",
        "hora":"11:00AM"
    },
    {
        "horaM":"1130",
        "hora":"11:30AM"
    },
    {
        "horaM":"1200",
        "hora":"12:00PM"
    },
    {
        "horaM":"1230",
        "hora":"12:30PM"
    },
    {
        "horaM":"1300",
        "hora":"01:00PM"
    },
    {
        "horaM":"1330",
        "hora":"01:30PM"
    },
    {
        "horaM":"1400",
        "hora":"02:00PM"
    },
    {
        "horaM":"1430",
        "hora":"02:30PM"
    },
    {
        "horaM":"1500",
        "hora":"03:00PM"
    },
    {
        "horaM":"1530",
        "hora":"03:30PM"
    },
    {
        "horaM":"1600",
        "hora":"04:00PM"
    },
    {
        "horaM":"1630",
        "hora":"04:30P.M."
    },
    {
        "horaM":"1700",
        "hora":"05:00P.M."
    }
]

}

And i would like to add a href link based on all the Json inside horarios. This is for a HTML form to make a POST to my api, i want to display "hora" in the List view and when clicked set the horaM value for using that selection to post.

喜欢这张图片。

I know i have to use Javascript DOM like this but cant get to work the onclick on the href pass that value to the variable and marked it as selected.

I was thinking maybe something like this to get the value.

<!DOCTYPE html>
<html>
<body>

<p>
<ul id="loadListview" data-role="listview" data-inset="true" >
  <li><a href="#" id="1200" onclick="myFunction()">12:00PM</a></li>
  <li><a href="#" id="1230" onclick="myFunction()">12:30PM</a></li>  
  <li><a href="#" id="1300" onclick="myFunction()">1:00PM</a></li>  
  <li><a href="#" id="1330" onclick="myFunction()">1:30PM</a></li>  
</ul>

<p id="demo"></p>

<script>
function myFunction() {
    var x = this.href;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

But im getting undefined value.

Thanks in advance for all your kind help and i greatly appreciate you taking the time for reading this.

In order to tell myFunction which a is clicked, you need to pass this to the onclick function, so it will look like this onclick="myFunction(this)"

<ul id="loadListview" data-role="listview" data-inset="true" >
    <li><a href="#THIS_HREF" data-hora="1200" onclick="myFunction(this)">12:00PM</a> </li>
</ul>
<p id="demo">x</p>

and then in the function add argument a . Also i will recommend using data-{attribute} like so

<script>
function myFunction(a) {
 var x = a.href; // "a" is the clicked element
 document.getElementById("demo").innerHTML = x;
 console.log(a.dataset.hora) // here is the data-hora attribute
 return false;
}
</script>

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