将链接值传递给ajax函数onclick事件

时间:2012-04-18 10:42:48

标签: php javascript html ajax

我有一个包含一些数据的链接

例如

<li><a href="" onclick="getcategory(this);"><?php echo $result22['category']; ?></a></li>

我希望此链接将此$result22['category'];值传递给ajax函数  我正在尝试这个

<script type="text/javascript">
function getcategory(cat)
{
var id = cat.value;
alert("hi" + id);
}
</script>

但它在警告框中显示未定义

出错了什么? 我没有在警告框中获得$result22['category'];的正确值

5 个答案:

答案 0 :(得分:3)

由于cata元素,因此它不具有value属性。如果可能有子元素,请使用textContentinnerText(或innerHTML):

function getcategory(cat) {
    var id = cat.textContent;
    alert("hi" + id);
}

通常只有具有value属性的表单控件(例如input元素)。

答案 1 :(得分:0)

为什么不这样做?

<li><a href="" onclick="getcategory('<?= $result22['category']; ?>');"><?php echo $result22['category']; ?></a></li>

<script type="text/javascript">
function getcategory(content) {
  alert(content);
}
</script>

答案 2 :(得分:0)

试试这个..

<li><a href="" id=<?php echo $result22['category']; ?> onclick="getcategory(this.id);"><?php echo $result22['category']; ?></a></li>

 <script type="text/javascript">
  function getcategory(cat)
  {
       //var id = cat.value;
       alert("hi" + cat);
  }
  </script>

答案 3 :(得分:0)

如果你没有使用单独的javascript文件绑定onClick处理程序,我建议你输出到函数中:

<li>
    <a href="" onclick="getcategory('<?php echo $result22['category']; ?>');">
        <?php echo $result22['category']; ?>
    </a>
 </li>

答案 4 :(得分:0)

<a href="" onclick="getcategory(this); return false;">hello</a>
<script type="text/javascript">
    function getcategory(ele) {
        var id = ele.innerHTML;
        alert(id);
    }
</script>
相关问题