javascript 下拉列表通过 AJAX 调用发送值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19962030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:22:28  来源:igfitidea点击:

drop down list send values through AJAX call

javascriptphpjqueryajax

提问by user2925595

I'm new to AJAX and trying to learn basic calls through AJAX and jQuery. I have a simple drop list of countries, where I want to select a particular country and send the value of it to server, where it would process which country was selected and give particular output. For now, it could just echo simple output in php file. There is some kind of problem with this code. I would appreciate if anyone could help me with it. thanks

我是 AJAX 的新手,正在尝试通过 AJAX 和 jQuery 学习基本调用。我有一个简单的国家/地区下拉列表,我想在其中选择一个特定国家/地区并将其值发送到服务器,服务器将处理选择的国家/地区并提供特定输出。现在,它只能在 php 文件中回显简单的输出。这段代码有一些问题。如果有人可以帮助我,我将不胜感激。谢谢

 <html>
 <head>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script type="text/javascript">
 function load()
 {
      $.ajax({
      type:"GET",
      url: "test.php",
      data: { country: $("#country").val()}
      }).done(function(msg){
       $("#right #myDiv").html(msg);
      });
 }

 </script>
 </head>
 <body>

 <div id="main">
    <div id="left">
          Select Country: <select id="country" name="country">
            <option value="germany">Germany</option>
            <option value="england">England</option>
          </select>
          <input type="button" value="Run Query" onClick="load()"></input>
    </div> 

   <div id="right">
      <div id="myDiv"></div>  
   </div>  
 </div>


test.php

测试.php

 <?php
      $name=$_GET['country'];
      if($name=="England")
      {
       echo "Works"; 
      }
      else
      {
      echo "doesnt Work"; 
      }
 ?>
 ?>

回答by dave

The only issue I see is that in your PHP you have

我看到的唯一问题是在你的 PHP 中你有

if($name=="England")

But in your html you have

但是在你的 html 你有

<option value="england">England</option>

It is sending over "england". "england"!= "England".

它正在发送“英格兰”。 "england"!= "England".

Also, I would just do

另外,我只会做

$("#country").val()

instead of

代替

$("#country option:selected").val()

回答by CtrlX

There is 2 errors in your code :

您的代码中有 2 个错误:

 data: { country: $("#country option:selected").val()}

should be $("#country").val()

应该是 $("#country").val()

and

if($name=="England")

shoudl be "england"

应该是“英格兰”

So Here is the right code :

所以这是正确的代码:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function load()
{

  $.ajax({
  type:"GET",
  url: "test.php",
  data: {     country: $("#country").val()    }
  }).done(function(msg){
   $("#right #myDiv").html(msg);
  });

 }

 </script>
</head>
<body>

<div id="main">
<div id="left">
      Select Country: <select id="country" name="country">
        <option value="germany">Germany</option>
        <option value="england">England</option>
      </select>
      <input type="button" value="Run Query" onClick="load()"></input>
</div> 





And for the test.php :

而对于 test.php :

 <?php
  $name=$_GET['country'];
  if($name=="england")
  {
   echo "Works"; 
  }
  else
  {
  echo "doesnt Work"; 
  }
 ?>

EDIT : It's pretty straight forward for me, but : be sure you call your page via your web server. The url in your browser should be :

编辑:这对我来说非常简单,但是:请确保您通过网络服务器调用您的页面。浏览器中的 url 应该是:

http://localhost/whatever/page.html

and not (if from your hard drive) :

而不是(如果来自您的硬盘):

file://c:/whatever/page.html

回答by Kaveh

if you mean too get work message from php file replace Englandwith england, because php is case sensitive.

如果您的意思也是从 php 文件中获取工作消息,请替换Englandengland,因为 php 区分大小写。