简体   繁体   English

单ID在while循环中同时提醒

[英]single id alerting same time in a while loop

I am having a table from where i am fetching activities and displaying in a table ...like this 我有一张桌子,我可以从那里取活动并显示在桌子上……就像这样

<table width="1000px;" style="border:0px;" >
    <tr>
        <?php
            $sql_activities="select * from tb_activities";
            $query_activities=mysql_query($sql_activities);
            while($row_activities=mysql_fetch_array($query_activities))
            {
        ?>
        <td width="50">
            <input type="radio" value="<?php echo $row_activities['activity_name']; ?>" name="activities" onclick="hi() " id="activities" /><?php echo " ".$row_activities["activity_name"]; ?></td>
            <?php    
            } 
            ?>
    </tr>
</table>

Now i have applied it in an onlick event of a radio button and the script for the function is: 现在,我将其应用于单选按钮的onlick事件中,该函数的脚本为:

<script type="text/javascript">
function hi()
{
    a = document.getElementById("activities").value;
    alert(a);
}

</script>

i want to alert the name of the activity chosen but when i click on any activity, it shows the same. 我想提醒所选活动的名称,但是当我单击任何活动时,它都显示相同的名称。 The first activity.ven if i have clicked on any other activity...can anyone help me ?? 第一次活动。即使我单击了其他任何活动,任何人都可以帮助我吗?

what you should do is change this: 您应该做的是更改此:

onclick="hi()"

to this: 对此:

onclick="hi(this);

then your function would be: 那么您的功能将是:

function hi(who) {
    var a = who.value;
    alert(a);
}

In PHP side of things change: 在PHP方面,事情发生了变化:

<td width="50"><input type="radio" value="<?php echo $row_activities['activity_name']; ?>" name="activities" onclick="hi() " id="activities" /><?php echo " ".$row_activities["activity_name"]; ?></td>

To: ( YOU NEED Unique IDs on Input/ Radio Buttons to be generated dynamically & Sent to JS) 到:(您需要动态生成输入/单选按钮上的唯一标识并将其发送给JS)

<?php $i = 1; ?>
<td width="50"><input type="radio" value="<?php echo $row_activities['activity_name']; ?>" name="<?php echo $row_activities['activity_name'].$i; ?>" id ="<?php echo $row_activities['activity_name'].$i; ?>" onclick="hi("<?php echo $row_activities['activity_name'].$i; ?> ")" /></td>
<?php $i++; ?>

In Javascript Change to This: 在Javascript中更改为:

function hi(elementID) {
    var value = document.getElementById(elementID).value;
    alert(value);
}

Code may have some escape errors, but logically this should give you idea how to do it, Hope that helps. 代码可能会有一些转义错误,但是从逻辑上讲,这应该使您知道如何执行此操作,希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM