0

这是我的html代码

<select name="course" id="course" onchange="valuesOfAll(this.value)">
   <option value=""> select </option>
   <option value="1"> Diploma in Computing</option>
</select>
<input name="course_credits" id="course_credits" type="text" />

我的数据库表是这样的

courseId   courseName            courseCredits
  1        Diploma in Computing    5

所以我的要求是,如果我更改“选择”中的值,“课程学分”值应该出现在文本框中。为此,我该如何编写 jquery 代码?

4

4 回答 4

1

将 html 与脚本分开是一种很好的做法,所以我想更改:

<select name="course" id="course" onchange="valuesOfAll(this.value)">

<select name="course" id="course" >

然后我的脚本将跟随(希望您添加最新 jquery 的参考)

<script>
$(document).ready(function(){
//bind change event once DOM is ready
$('#course').change(function(){});
getResult($(this).val());

});


function getResult(selectedValue){
//call  ajax method to get data from database
 $.ajax({
            type: "POST",
            url: url,//this  should be replace by your server side method
            data: "{'value': '"+ selectedValue +"'}", //this is parameter name , make sure parameter name is sure as of your sever side method
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (Result) {
               alert(Result.d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                  alert(errorThrown);
            }
        });
}
</script>
于 2013-03-22T06:58:14.010 回答
1

“Ajax with Jquery”是您正在寻找的。它将像这样工作:

  • 用户从选择框中选择一个选项
  • 您通过 Javascript 将所选选项提交给 PHP 脚本
  • php 脚本从数据库中获取数据
  • php 脚本将结果作为 json 编码数据返回
  • 您的 javascript 代码中有一个回调函数。此 js 代码将以您想要的任何方式操作 HTML,例如“将选项添加到您的选择框”

有大量关于如何执行 Ajax 请求的详细教程,例如http://openenergymonitor.org/emon/node/107

查看其中一个教程 - 最终你会想要更新你的问题,让它变得更具体一点?:-)

于 2013-03-22T06:59:51.783 回答
0

使用$.post..(ajax 或 get)...我在这里使用 post..如果您想阅读有关 jquery post 的更多信息,请查看文档..

javascript

 function valuesofAll(val){
     $.post('test.php',{data:val},function(result){
         $('#course_credits').val(result.coursecredit)
     }, "json"); //expect response as json..
 }

测试.php

$data=$_POST['data']; //this is the value posted 
//make you query in db get result..
$courseCredits= $row; //db returned courseCreadit value
echo json_encode(array('coursecredit'=>$courseCreadits)); //send response as json
于 2013-03-22T06:56:14.607 回答
0

这将帮助你......

<script>
function valuesOfAll(value)
{
var base_url="http://.../../hello.php";
var ip=new Object();
ip.course=value;
var inputParam=JSON.stringify(ip);
var module="getcourseCredits"
 $.ajax({
                     type: "POST",
                 url: base_url,
                 data: {coursevalue:inputParam,module :module}, 
                     dataType: "json",
                     success: function(msg)
                     {
                           $("#course_credits").val(msg.credit);   
                         }
         });
}

</script>
<body>
<select name="course" id="course" onchange="valuesOfAll(this.value)">
   <option value=""> select </option>
   <option value="1"> Diploma in Computing</option>
</select>
<input name="course_credits" id="course_credits" type="text" />
</body>

在你的 PHP 文件中

<?php
if(isset($_POST['module']))
{
    if($_POST['module']=='getcourseCredits')
    {
           $val=json_decode($_POST['coursevalue'],true);
            $courseval=$val['course'];
           // do the connectivity and query here and finally echo the result as json format that is the response to the ajax call 
             .
             .
             .
             .

        }
}
?>
于 2013-03-22T07:12:50.270 回答