【发布时间】:2015-10-07 21:54:36
【问题描述】:
这是来自 php 数据库的数据的列表表示形式。使用 foreach php 循环获取列表,并相应地填充 achor 标记内的数据。
<?php foreach($array as $iterate):?>
<div class="col-sm-3">
<div class="panel panel-default">
**<a onClick='theFunction();' id="hello" href="#myModal" style="text-decoration:none;">**
<div class="panel-heading">
<img src ="audi_sillhouete.jpg" style="height:100%; width:100%;">
<p id="10" style="position:absolute; top:10%; padding-left:5%; padding-right:15%;"><?php echo $iterate->test_name ?></p>
</div>
</a>
</div>
</div>
<?php endforeach;?>
正如您在此处看到的,在锚标记内,我已将其href-ed 到一个模式框,每当用户单击任何链接时都会显示一条消息。模态窗口中有一个按钮,可以将用户带到不同的页面。 问题是我想与 url 一起传递某些值,但不能这样做,因为到下一页的链接是在模态窗口规范部分中定义的。 所以我想出了这个解决方案。我想到了使用锚标记的 id 属性,我试图获取使用 javascript 单击的锚的 id 属性。
<script type="text/javascript">
function theFunction()
{
var id = $('a', this).attr('id');
alert(id);
}</script>
由此我想初始化一个 php 变量,然后使用该 php 变量作为模式窗口按钮的 href 中的值传递。但是由于某种原因,我在警报框中得到的值是“未定义的”。我已经尝试了所有可能的组合。
$('a',this).attr('id');
$this.id;
一切都返回“未定义”。
参考-这是模态窗口部分的代码。
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Instructions</h4>
</div>
<div class="modal-body">
Your test is about to commence. A timer will be initiated after the moment you start the test. You may choose to answer a question, or leave it empty. You can also attempt the questions in any order that you find suitable.<br><br>Upon completion of the test, click on "Submit" to see your performance statistics.<br><br><br>Good luck!
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<a href="../test/pretest.php?test=<?php echo $testname?>" style="text-decoration:none;color:white;"><button type="button" class="btn btn-primary">Continue</button></a>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
【问题讨论】:
-
theFunction() 的上下文是什么?我认为“这”是您代码中的问题。
-
将
this传递给您的函数调用。还要避免内联函数调用 -
如何在函数调用中添加这个引用?
标签: javascript php jquery html