jQuery函数未在PHP foreach循环内执行 [英] jQuery function not executing inside PHP foreach loop

查看:157
本文介绍了jQuery函数未在PHP foreach循环内执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生一些复选框的过滤器面板选中时会刷新的产品采取了具体的参数考虑名单.

I'm trying to generate a number of checkboxes for a filter panel that when checked will refresh a list of products taking a specific parameter into account.

在foreach循环中,我通过从API调用的xml数据中收集过滤器属性来成功创建复选框

In a foreach loop, I'm successfully creating the checkboxes by gathering the filter attributes from xml data called from an API

foreach ($attrib->attributeValues->attributeValue as $attribValue)
{
$attribValue = $attribValue->name;
$attribValueID = $attribValue[@id];

$BODY .= sprintf("<input type=\"checkbox\" name=\"%s\" value=\"%s\" id=\"%s\" checked%s=\"checked\" /> %s ",
$attribClass, $attribValue, $attribValue, $attribValue, $attribValue);

接下来,我将使用两个简单的警报来测试jQuery的基本功能,以确保实际调用了这些功能

Next, I'm testing a basic jQuery function with two simple alerts to insure that the functions are actually being called

$BODY .= "<script type=\"text/javascript\">";
$BODY .= "alert('Hello world!');";
$BODY .= "$(\"#".$attribValue."\").change(function() {";
$BODY .= "var $input = $(this);";
$BODY .= "alert(\"".$attribValue."\");";
$BODY .= "}).change();</script>";

当我在页面上运行此代码时,呈现的html源代码明确显示正在生成jQuery函数(多次,每个创建的复选框一个),但是我没有看到任何一个警报-外部警报功能或内部功能之一.这是我第一次使用jQuery-有什么想法吗?一如既往的感谢!

When I run this on the page, the rendered html source code definitely shows that the jQuery function is being generated (multiple times, one for each checkbox created), but I'm not seeing either of the alerts - the one outside the function, or the one inside. It's my first time working with jQuery - any thoughts? Thanks as always!

抱歉,这是为两个复选框生成的HTML源代码-对于所有存在的复选框(两位数),此操作将继续.

Sorry, here's the generated HTML source code for two checkboxes - this continues for all the checkboxes present, which are in the double digits.

<input type="checkbox" name="Color" value="Black" id="Black" checkedBlack="checked" /> Black <script type="text/javascript">alert('Hello world!');$("#Black").change(function() {var  = $(this);alert("Black");}).change();</script>
<input type="checkbox" name="Color" value="Silver" id="Silver" checkedSilver="checked" /> Silver <script type="text/javascript">alert('Hello world!');$("#Silver").change(function() {var  = $(this);alert("Silver");}).change();</script>

推荐答案

这是我更新的修复程序.

Here is my updated fix.

$BODY .= "<script type=\"text/javascript\">";
$BODY .= "$(document).ready(function(){";
$BODY .= "alert('Hello world!');";
$BODY .= "$(\"#".$attribValue."\").live('click',function() {";
$BODY .= "var \$input = $(this);";
$BODY .= "alert(\"".$attribValue."\");";
$BODY .= "}).click();});</script>";

您可以在小提琴

我确实必须写$(document).ready()以及在php中转义$input,以确保它不呈现为php字符串.我也将.change()更改为.live('click',.click()

I did have to write in the $(document).ready() as well as escape the $input in php to ensure it is not rendered as a php string. I also changed the .change() to .live('click', and .click()

活动部分将确保看到动态加载的复选框.我只是喜欢单击事件,可以根据需要将其设置为更改.

the live part will ensure dynamically loaded checkboxes are seen. I just prefer the click event, you can have it set to change if you prefer.

干杯!

这篇关于jQuery函数未在PHP foreach循环内执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆