2012-11-03 2 views
0

페이지 당 4 개의 행이 있습니다. mysql 테이블을 직접 구문 분석하는 네 개의 행. 각 행에는 확인란이 있습니다. 어떤 체크 박스가 선택되었는지 알고 싶습니다. 아래 표시된 코드에서 페이지의 첫 번째 행만 저장합니다. 따라서 세 번째 행을 선택하면 선택한 첫 번째 행으로 표시됩니다. 나는 뭔가를 선택했는지 알기 위해 onchange 메서드를 사용하고 있지만 아직 어떻게 가져올 지 모릅니다.어떤 확인란이 선택되었는지

누군가 도움을 줄 수 있습니까?

<?php 

    //limit for number of categories displayed per page 
    $limit = 4; 

    $categoriesNum= mysql_query("SELECT COUNT('categoryTopic') FROM categories"); 

    //number of current page 
    $page =(isset($_GET['page']))? (int) $_GET['page'] :1; 

    //calculate the current page number 
    $begin =($page - 1)* $limit; 

    //number of pages needed. 
    $pagesCount =ceil(mysql_result ($categoriesNum,0)/$limit); 

    //Query up all the Categories with setting the Limit 
    $CategoryQuery = mysql_query ("SELECT categoryTopic From categories ORDER BY categoryTopic LIMIT $begin, $limit"); 

    //Place all categories in an array then loop through it displaying them one by one 
    while ($query_rows = mysql_fetch_assoc($CategoryQuery)) 
    { 

     $category =$query_rows['categoryTopic']; 
     //echo $category; 
     //query all the subcategories that the current category has 
     $Sub = mysql_query ("SELECT categoryTopic FROM subcategories WHERE categoryTopic='$category'");    
     $Count = mysql_num_rows ($Sub); 

     echo '<table width="85%" border="1" cellpadding="0"; cellspacing="0" align="center"> 

     <tr>   
      <th width="23%" height="44" scope="col" align="left"> '.$query_rows['categoryTopic'].' <br><br><br></th> 
      <th width="24%" scope="col">'.$Count.'</th> 
      <th width="25%" scope="col"> 0 </th> 
      <th width="28%" scope="col"> <form name = "choose"> 
      <label><input type="checkbox" id ="check" value= '.$category.' onchange="handleChange(this);"></label> 
     </tr> 
    </table>'; 
    } 
    ?> 

<script type="text/jscript"> 
//this function will be called when user checks a check box. 

function handleChange(cb) { 

//get the selected category 
var category = document.getElementById('check').value; 
document.write(category); 

답변

1

모습의 당신이 페이지에서 ID가 고유해야하기 때문에 중복 ID의 페이지에 ..

행의 각이 LABEL ID를 가지고있는 경우는 =,를 "확인"가 발생하는 등의 그것은 항상 해당 ID와 함께 첫 번째 요소를 얻을 것이다 ..

을 그래서 항상 첫 번째 행이 될 것입니다 ..

대신

이벤트에 직접 this.value 전달 시도 체크 박스의

onchange="handleChange(this.value); 

function handleChange(cb) { 

document.write(cb); 

이 .. 그래서 먼저 중복 ID의 문제를 해결하려고 나쁜 관행입니다 ..

+0

으로 변경해야합니다. 각 행에 다른 행을 부여하면 어떻게 하나를 클릭했는지 알 수 있습니까? – user1785939

+0

Beautiful 아름답게 완성되었습니다. 고마워요, 굉장 해요. – user1785939

1

나는 당신의 질문은 바로, 당신은 당신이 범주를 얻을 수있는 방법을 알고 싶어요 이해한다면 (값) 사용자가 방금 클릭 한 체크 박스?

function handleChange(cb) { 
    // get the selected category 
    var category = cb.value; 
    // ... 
} 

당신은 cb -param를 통해 handleChange α- 함수 현재 클릭 체크 박스를 제공합니다. 따라서이 체크 박스의 속성을 가져 오려면 cb -object의 속성을 찾으십시오.

0

HTML이 유효하지 않게하는 동일한 ID를 가진 행이 여러 개 있습니다. 이 문제를 방지하려면 자바 스크립트 코드를

function handleChange(cb) { 
//get the selected category 
var category = cb.value; 
document.write(category); 
} 
관련 문제