简体   繁体   English

如何使用来自json的数据填充href标记,并在单击时使用表单中的选定值进行发布

[英]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. 我想基于horios中的所有Json添加href链接。 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. 这是用于HTML表单以对我的api进行POST,我想在“列表”视图中显示“ hora”,然后单击以设置horaM值以使用该选择进行发布。

喜欢这张图片。

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. 我知道我必须像这样使用Javascript DOM,但是无法使用href上的onclick将该值传递给变量并将其标记为选中状态。

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)" 为了告诉myFunction其中a点击,你需要通过this来的onclick功能,所以它看起来是这样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 . 然后在函数中添加参数a Also i will recommend using data-{attribute} like so 我也建议像这样使用data-{attribute}

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择选项标签中的值时如何从json数组中获取更多数据 - How to get more data from json array when a value in option tag is selected jquery 单击AJAX时如何显示所选选项标签的值 - How to display the value of the selected option tag when clicked AJAX 如何从javascript的href标记中的json数据添加url - how to add url from json data in the href tag in javascript 单击链接时,从Android WebView中的锚标记获取href值 - Get href value from anchor tag in Android WebView when link is clicked 单击锚标记时如何验证表单中的数据 - How do I validate data in a form when an anchor tag is clicked 如何使用jQuery获取单击的href标签的数据属性 - How to get the data attribute of clicked a href tag using jquery 单击子div时如何停止父href标签的重定向? - how to stop redirection of parent href tag when clicked on child div? 如何从此自定义HTML表单标签中找到“当前选定的值”? - How to find Currently Selected value from this Custom HTML form Tag? 单击超链接时,如何将href的值传递给javascript并在window.location中使用它? - How to pass value of href to javascript and use it in window.location when a hyperlink is clicked? 单击li标签中的href时链接未打开 - Link not opening when clicked on the href in li tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM