jQuery函数没有在PHP foreach循环中执行
jQuery function not executing inside PHP foreach loop
我正在尝试为筛选面板生成一些复选框,当选中这些复选框时,将刷新考虑特定参数的产品列表。
在foreach循环中,通过从API
调用的xml数据中收集过滤器属性,我成功地创建了复选框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函数,以确保函数实际上被调用
$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——有什么想法吗?一如既往的感谢!
编辑:对不起,这是为两个复选框生成的HTML源代码-所有复选框都是两位数的。
<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>
这是我更新的修复。
$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()
活动部分将确保看到动态加载的复选框。我更喜欢click事件,如果你愿意,你可以把它设置为改变。
干杯!
我想你在这里覆盖了$attribValue
: $attribValue = $attribValue->name;
-你应该使用一个不同的变量来存储该值:$myAttribValue = $attribValue->name;
你不需要在这里使用jQuery来使它过于复杂。
使用:
function filterToggle(element){
var elementII = document.getElementById(element)
if(elementII.checked){
alert("Checked");
}
else{
alert("Unchecked");
}
}
然后在复选框中添加onclick="filterToggle('%s')"
相关文章:
- 如何在foreach循环中对每个产品单独应用评级系统
- 错误:Can't在从forEach循环发送标头后设置标头
- Foreach循环只返回最后一个值
- promise.all在一个forEach循环中——所有东西都同时启动
- Foreach循环添加值略有偏离
- 如何让foreach循环等待$http.get完成后再继续
- 需要帮助比较数组内的值,使用forEach循环
- 使用从forEach循环中提取的国家代码从数组中获取国家名称
- 将 PHP 值 (ACF) 添加到 foreach 循环中的 Javascript 数组中
- 如何使用 foreach 循环在表中重复单选按钮
- php在foreach循环中创建javascript
- foreach循环每次迭代之间的延迟
- 得到"TypeError:对象不是函数“”;在forEach循环中使用超级测试/超级代理时
- 在forEach循环中创建promise,并在不嵌套的情况下使用它们
- 我的单选按钮没有从foreach循环中获得值,表单验证返回'必填字段'即使已检查
- 我如何做一个纯Javascript foreach循环并打印出结果firebase
- PHP和Ajax在回调和返回数组中使用foreach循环
- 在javascript中的foreach循环中捕获PHP var,并在其他PHP页面中使用它
- Wordpress:Jquery.html函数现在在PHP foreach循环中执行
- foreach循环中的Javascript倒计时时钟