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

问题描述 投票:0回答:1

希望大家度过一个美好的一天。我正在研究一个项目,我似乎无法做到这一点。我有以下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."
    }
]

}

我想添加一个基于hson里面所有Json的href链接。这是一个HTML表单对我的api进行POST,我想在List视图中显示“hora”,当点击时设置horaM值以使用该选择发布。

Like this image.

我知道我必须像这样使用Javascript DOM但是无法在href上工作onclick将该值传递给变量并将其标记为已选中。

我想也许是这样的东西来获得价值。

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

但我得到了不明确的价值。

在此先感谢您的所有帮助,我非常感谢您花时间阅读本文。

javascript jquery html html5 dom
1个回答
0
投票

为了告诉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>

然后在函数中添加参数a。另外我会建议像这样使用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>
© www.soinside.com 2019 - 2024. All rights reserved.