脚本不工作在阿贾克斯返回的值脚本、工作、阿贾克斯

2023-09-11 00:34:50 作者:我的温柔只给你

我展示产品ID和title.there也是一个链接此成员的演出项目。而点击它就会显示user.I的项目正在使用AJAX的this.There是用于检测脚本点击checkbox.This正常工作为先的顺序列出items.but不工作的AJAX返回的项目

 <表ID =结果WIDTH =100%>
< PHP
如果($ tot1大于0){
而($ row5 = mysql_fetch_array($ RESULT1))
{
?>
&所述; TR类=detail9txt高度=30>
< TD宽度=2%><输入类型=复选框名称=项值=< = $ row5 [ITEM_ID];>中>< / TD>
< TD ALIGN =中心WIDTH =12%的风格=垂直对齐:基线;>< A HREF =>< = $ row5 ['ITEM_TITLE'];>有&所述; / a取代;
< / TD>
< / TR>
< TR>< TD合并单元格=6ALIGN =权与GT;< A HREF =JavaScript的:(?< = $ row5 [USER_ID];>)callfunc的风格=字体大小:10px的;>显示项目这位议员和LT; / A>< / TD>< / TR>
< PHP
}}
?>
< /表>
<输入名称=hidden_​​field类型=隐藏ID =hidden_​​field/>
<输入类型=形象NAME =提交SRC =图像/ submito.gifVALUE =提交>
 

脚本code和AJAX是

 <脚本类型=文/ JavaScript的>
功能callfunc(STR)
{

//警报(STR);
如果(str.length == 0)
{
。的document.getElementById(结果)的innerHTML =;
返回;
}
如果(window.XMLHtt prequest)
{// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
XMLHTTP =新XMLHtt prequest();
}
其他
{// code对IE6,IE5
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能()
{
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
{
的document.getElementById(结果)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,getdetails.php CID =?+ STR,真正的);
xmlhttp.send();
}
< / SCRIPT>
&所述;脚本的src =的http://$c$c.jquery.com/jquery-latest.js>&所述; /脚本>

<脚本类型=文/ JavaScript的>
<! - 
$(文件)。就绪(函数(){

    变种阵列= [];
    $('输入[名称=项])。点击(函数(){

        如果($(本).attr('检查')){
            //添加新的元素,如果检查:
            array.push($(本).attr('值'));
            警报(阵列);
            //hidden_​​field.value = array.join(,);
            //hidden_​​field.value = array.join(,);
            。的document.getElementById('hidden_​​field')值= array.join(,);
        }
        其他 {
            如果不加以控制//删除元素:
            对于(VAR I = 0; I< array.length;我++){
                如果(数组[我] == $(本).attr('值')){
                    array.splice(ⅰ,1);
                }
            }
        }


    });
});
//  - >
< / SCRIPT>
 

getdetails.php code是

 < PHP
需要'包括/将connect.php;
$ CID = $ _ GET ['CID'];

$ SQL =SELECT * FROM表,其中user_ID的= $ CID;
$水库=的mysql_query($ SQL);
$ TOT = mysql_num_rows($水库);
如果($托特大于0){
而($行= mysql_fetch_array($水库))
{
回声'&所述; TR类=detail9txt高度=30>
< TD宽度=2%><输入类型=复选框名称=项值='。$行[ITEM_ID]。'>< / TD>
      < TD ALIGN =中心WIDTH =12%的风格=垂直对齐:基线;>
        &所述; A HREF =>'。$行['ITEM_TITLE']'&所述; / a取代;
      < / TD> < / TR>';
}}
?>
 
区块链学习笔记8 BTC比特币脚本

解决方案

使用这个脚本

 <脚本类型=文/ JavaScript的>
变种阵列= [];
    $('#项目)。住(点击,函数(){
如果($(本).attr('检查')){
            //添加新的元素,如果检查:
            array.push($(本).attr('值'));
            。的document.getElementById('hidden_​​field')值= array.join(,);
        }
        其他 {
            如果不加以控制//删除元素:
            对于(VAR I = 0; I< array.length;我++){
                如果(数组[我] == $(本).attr('值')){
                    array.splice(ⅰ,1);
                }
            }
        }


});

< / SCRIPT>
 

I am displaying item id and title.there is also a link "show items of this member".while clicking on it it will display the items of that user.I am using ajax for this.There is a script for detecting the order of clicking on checkbox.This working properly for first listed items.but not working for ajax returned items

<table id="result" width="100%">
<?php 
if($tot1>0){
while($row5=mysql_fetch_array($result1))
{
?> 
<tr class="detail9txt" height="30"> 
<td width="2%"><input type="checkbox" name="item" value="<?=$row5[item_id];?>"></td>
<td align="center" width="12%" style="vertical-align:baseline;"><a href=""><?=$row5['item_title'];?></a>
</td> 
</tr>
<tr><td colspan="6" align="right"><a href="javascript:callfunc(<?= $row5[user_id]; ?>)" style="font-size:10px;">Show items for this Member</a></td></tr>
<?php
}}
?>
</table>
<input name="hidden_field" type="hidden" id="hidden_field"/>
<input type="image" name="submit" src="images/submito.gif" value="submit">

script code and ajax is

<script type="text/javascript">
function callfunc(str)
{

//alert(str);
if (str.length==0)
{ 
document.getElementById("result").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getdetails.php?cid="+str,true);
xmlhttp.send();
}
</script>
<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function () { 

    var array = []; 
    $('input[name="item"]').click(function () { 

        if ($(this).attr('checked')) { 
            // Add the new element if checked: 
            array.push($(this).attr('value')); 
            alert(array);
            //hidden_field.value = array.join(',');
            //hidden_field.value = array.join(',');
            document.getElementById('hidden_field').value=array.join(',');
        } 
        else { 
            // Remove the element if unchecked: 
            for (var i = 0; i < array.length; i++) { 
                if (array[i] == $(this).attr('value')) { 
                    array.splice(i, 1); 
                } 
            } 
        } 


    }); 
});
//-->
</script>

getdetails.php code is

<?php
require 'include/connect.php';
$cid=$_GET['cid'];

$sql="select * from table where user_id=$cid";
$res=mysql_query($sql); 
$tot=mysql_num_rows($res);
if($tot>0){
while($row=mysql_fetch_array($res))
{
echo '<tr class="detail9txt" height="30"> 
<td width="2%"><input type="checkbox" name="item" value="'.$row[item_id].'"></td>
      <td align="center" width="12%" style="vertical-align:baseline;">
        <a href="">'.$row['item_title'].'</a>
      </td> </tr>';
}}
?>

解决方案

Use this script

<script type="text/javascript">
var array = []; 
    $('#item').live('click', function() {
if ($(this).attr('checked')) { 
            // Add the new element if checked: 
            array.push($(this).attr('value')); 
            document.getElementById('hidden_field').value=array.join(',');
        } 
        else { 
            // Remove the element if unchecked: 
            for (var i = 0; i < array.length; i++) { 
                if (array[i] == $(this).attr('value')) { 
                    array.splice(i, 1); 
                } 
            } 
        } 


});

</script>