将id值传递给jquery

时间:2015-04-13 05:26:16

标签: php jquery html

我正在尝试在我的jquery函数中传递a链接标记的id,但它无法正常工作。这是我的代码,

<script>
$(document).ready(function() {
        var guyid = $('.guyid').attr('id');
        $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicDay'
        },
        editable: true,
        eventLimit: true,
        eventSources: ['json-script.php?id=guyid']
    });
});
</script>
<body>
<?php
    $result_guy = mysql_query("SELECT id, name FROM person");
    while ($row_guy=mysql_fetch_array($result_guy)) {
        echo '<a href="index.php" class="guyid" id="' .$row_guy[0]. '">' .$row_guy[1]. '</a><br>';
    }
?>
</body>

我的代码中有什么问题吗?如何获取a标记的id值。非常需要这个帮助。 TNX。

2 个答案:

答案 0 :(得分:3)

代替

 eventSources: ['json-script.php?id=guyid']

eventSources: ['json-script.php?id='+guyid]

答案 1 :(得分:0)

$(document).ready(function() {
        var guyid = $('.guyid').attr('id');
        $('#calendar').fullCalendar({
        ...
        eventSources: ['json-script.php?id=' + guyid] //<-- fix
    });
});

并且您必须知道该家伙始终拥有第一个 标记的ID。 例如,

<a href="index.php" class="guyid" id="1">1</a> 
<a href="index.php" class="guyid" id="2">2</a>
<a href="index.php" class="guyid" id="3">3</a>
<a href="index.php" class="guyid" id="4">4</a>

然后,你的傻瓜总是只有 1 。 如果你想获得所有的id,你必须使用每个循环。 恩。

 var arr = [];
 $('.guyid').each( function(idx, g) { 
  console.log( $(g).attr('id') );
  arr.push( $(g).attr('id') );
 });

并在eventSources上使用arr数组。

看起来像这样

   eventSources: ['json-script.php?id=' + arr.join()]

哈哈,点击标签时你想要制作链接吗? 然后,

    $(document).ready(function() {
        $('a').click( function(event) {
            event.preventDefault(); //if you don't want to go index.php
            var guyid = $(event.target).attr('id');
            // or 
            //var guyid = event.target.id;

            $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,basicDay'
            },
            editable: true,
            eventLimit: true,
            eventSources: ['json-script.php?id=' + guyid]
          });
        });
    });

对不起,我的英语很差。 如果这对你有帮助,我会很高兴,Tnx。