繁体   English   中英

如何从另一个页面获得价值

[英]how to get value from another page

我应该做一个动态选择框,其中医生姓名的选择取决于在另一个选择框即专业中选择的名字。

继承人的HTML

    <select name="docSpec" id="docSpec" onChange="getSpecialty('getSpec.php?spec='+this.value)">

            <option>pick a specialization</option>
            <option value="General">General</option>
            <option value="pediatrics">pediatrics</option>
            <option value="Physician">Physician</option>
            <option value="Cardiologist">Cardiologist</option>
            <option value="Pulmonary">Pulmonary</option>

    </select>   




        <div id="getDoc"> <!-- the contents from getSpec.php are displayed in this div! -->
            <select>
                <option>select doctor</option>
            </select>
        </div>

仍然在同一文件中,这是我的JavaScript。

    function getXMLHTTP() { //fuction to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}



function getSpecialty(strURL) {     

    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('getDoc').innerHTML=req.responseText;                       
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}

我有一个单独的文件包含这个名为getSpec.php的请求

<?php $spec=$_REQUEST['spec']; // i converted javascript variable to php by passing it to url
    require_once('dbcon.php');
    $query="select doctorName from doctors where specialty= '$spec'"; // this is why i passed it with jquery
    $result=mysqli_query($conn,$query);
?>
<select name="doctor">
    <option>Select doctor</option>
    <?php 

        while( $row = mysqli_fetch_row($result)) { 

            echo "<option value='$row[0]'>$row[0]</option>"; // for contents of the dropdown

       } ?>

我通过此代码传递了javascript变量并将其转换为另一页getSpec.php中的 php,但现在的问题是我无法从所选的<select name="docName>获取值。有办法可以得到吗?

您的浏览器是什么? 您在处理DOM时不能依赖innerHtml。 它在每种浏览器上的工作方式都不同,IE与基于Webkit的浏览器对XMLHTTPRESPONSE的处理方式也不同。 我认为它是“完成”或“完成”而不是4。

您用jquery标记了问题,为什么不使用此库解决问题。 $ .get和$ .ajax和$(“ some css selector”)。html('your new html')的成功处理程序是很好的工具。

编辑:我正在按要求添加更多详细信息。
如您所要求的,您可以使用下面的代码来获取select和getDoc的innerHtml :(“#” id代表id,“ [sth =?]”代表属性相等)

var old_get_doc_innerHtml = $('#getDoc').html();
var old_select_innerHtml = $('select[name="doctor"]').html();

专为您的getSpecialty(strURL)代码使用:

function getSpecialty(strURL) {
    var jqxhr = $.get(strURL, function (data) {
        // runs only if http fetch has succeeded.
        $("#getDoc").html(data);
        alert("Load was performed.");
    })
    .done(function () {
        // another success implementation type
    })
    .fail(function () {
        // runs only if http fetch has failed.
    })
    .always(function () {
        // runs always both in fail and success.
    });
}

好的,简短的代码简介:
$是jQuery的运算符,可以使用$.someFunction()使用静态函数。 jQuery的元素选择器使用“ CSS选择器”字符串,语法为: $('CSS_SELECTOR')
通过使用它,您可以选择页面上的所有“标签”标签: $('label')或具有type ='text' $('input[type="text"]')或更多类型的'input'标签。 请参阅API文档选择器页面

使用css_selector选择元素后,您可以运行特定于元素的函数,例如html() jQuery永远不会返回void,而是返回元素Object,因此我们可以在变量/ DOM元素上使用每个操作函数,而无需使用';'。 我将这个概念用于jqxhr变量(内部:getSpecialty(strURL),jqxhr是方法$ .get的返回对象),在这里我称为.done(...) .fail(...).always(...)jqxhr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM