提取innerhtml的值

时间:2016-11-09 11:22:41

标签: php jquery ajax

我有一个div元素id我的php页面

<div class="col-md-8"  id="sub_group" name="sub_group">
    <!--output from other file -->
    </div>

我已经创建了一个从get_course_details.php

中检索数据的函数
    <script type="text/javascript">
        var RequestObject = false;
        if (window.XMLHttpRequest) { 
          RequestObject = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
          RequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

    function get_innerHTML(targetID, requestData, targetPHP){
        if (RequestObject){
            RequestObject.open("POST", targetPHP);
            RequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

            RequestObject.onreadystatechange = function(){
                if (RequestObject.readyState == 4 && RequestObject.status == 200) {
                    targetID.innerHTML = RequestObject.responseText; //setting the returned data by server to the address element
                }
            }
            RequestObject.send(requestData); // sending scentre value to fetch scentre addres
        }
        return false; // returned false so that form do not send submit request
    }

I have used this function to get response from PHP

    $(document).ready(function() {
    $("#course").change(function(){
            if ($("#course").val()!=""){
                var requestData = "course_id=" + $("#course").val() ;
                get_innerHTML(sub_group, requestData, "./support/get_course_details.php"); //(targetID, requestData, targetPHP )
            }
            alert($( "#subroup" ).find( "optional" ).value); *// display undefined*
            $('#sub_group').checkboxes('max', 4); //here i want pass the value of optional text i.e. 4
        });

警报信息显示&#34;未定义&#34;   我无法从具有id&#34;#optional&#34;的文本框中获取值。   我正在检查警报方法   我已经看到警报消息在他们加载innerHTML之前出现。

如何在(&#39;#course&#39;)之后立即获得id的值(&#34;可选&#34;)。change()函数已完成

(get_course_details.php)代码是

    <?php
    require_once("../include/dbconn.php");
    $course_id = $_POST['course_id'];
    $statement1 = "select `eligibility`, `fees`, `optional` from course where `course_id`='$course_id';";
    $output1 ="";
    $output2 ="";
    $output3 ="";
    $output4 ="";
    $result1 = $link->query($statement1);
        if($result1){
            $row = $result1->fetch_assoc();
            $output1= $row['eligibility'];
            $output2= $row['fees'];
            $output3= $row['optional']+1;
        }
        else{echo "ERROR";}

    $statement2 = "SELECT group_id, group_name, group_type FROM pssou.`group` where course_id='$course_id';";
    $result2 = $link->query($statement2);
        if($result2){
            for ($row_no = 1; $row_no <= $result2->num_rows; $row_no++) {
                $row = $result2->fetch_assoc();
                if ($row['group_type']=="C"){ // if group_type is compulsory
                    $output4.= "<div class='form-check disabled'><label class='form-check-label'>";
                    $output4 .="<input class='form-check-input' name='group_id' id='group_id' value='".$row['group_id']."' type='checkbox' checked disabled/>". $row['group_name'];
                    $output4.= "</label></div>";
                }
                else{
                    $output4.= "<div class='form-check'><label class='form-check-label'>";
                    $output4.="<input class='form-check-input' name='group_id' id='group_id' value='".$row['group_id']."' type='checkbox'/>". $row['group_name'];
                    $output4.= "</label></div>";
                    }
            }
        }
        else{echo "ERROR";}

    $output = <<< EOF
    <div class="col-md-6">
    <div class="row">
    <label> Minimum Qualification </label>
    <div class="well well-sm" role="alert">
    <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
    $output1
    </div> 
    </div>
    <div class="row">
    <label> Course Fees </label>
    <div class="well well-sm" role="alert">
    <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
    $output2
    </div>
    </div>
    </div>
    <div class="col-md-6">
    <label> subject (Select Any $output3) </label>
    <input type="text" id="optional" value ="$output3"/>
    $output4
    </div>

    EOF;


        echo $output;
        $result1->free();
        $result2->free();

        ?>

1 个答案:

答案 0 :(得分:0)

看看你的专栏:

alert($( "#subroup" ).find( "optional" ).value)

1)是&#34; #subroup&#34;拼写错误?是否有一个元素id =&#34; subroup&#34;? (它不是 g roup?)

2).find()函数需要一个选择器,你传递了一个元素选择器,它试图找到一个名为&#34的元素;可选&#34;如果你想通过 id 找到一个元素,那么使用.find(&#34;#optional&#34;)

可能还有其他问题。

如果您正在使用jQuery,为什么不使用jquery.ajax()?