【问题标题】:reload div without refresh page重新加载div而不刷新页面
【发布时间】:2020-07-22 04:11:53
【问题描述】:

我有一个显示状态的选择选项 我有一个 DIV 返回所选州的运费 我的问题是当我选择一个选项并返回 DIV 的值时页面刷新 我想在不刷新整个页面的情况下显示DIV的值

    <html>
<body>
<div id ="content"> this is the cost of shipping of :
<?php 
    $ship=20;
    if (isset($_POST['city'])){
        $listings = $_POST['city'];
    } else if (isset($_GET['city'])) {
        $listings = $_GET['city'];
    } else {
        $listings = 'city';
    }
    switch ($listings) {
        case 'California':
            $ship=$ship+5; echo $ship;
            break;
        case 'Alabama':
            $ship=$ship+10; echo $ship;
            break;
        case 'Texas':
            $ship=$ship+15; echo $ship;
    }

?>
    </div>
    <form id = "form1" name = "form" >
        <select id ="city" name ="city" method="post"   onchange="this.form.submit()"    >
            <option value="California" <?php echo (isset($_POST['city']) && $_POST['city'] == 'California') ? 'selected="selected"' : ''; ?>>California</option>
            <option value="Alabama" <?php echo (isset($_POST['city']) && $_POST['city'] == 'Alabama') ? 'selected="selected"' : ''; ?>>Alabama</option>             
            <option value="Texas" <?php echo (isset($_POST['city']) && $_POST['city'] == 'Texas') ? 'selected="selected"' : ''; ?>>Texas</option>
    
        </select>
    
    </form>


</body>
</html>

java脚本

   <script type="text/javascript" >
$("#form1").click(function(){
    $("#content").load(" #content");
});
</script>

【问题讨论】:

  • 使用 ajax 。你试过了吗?如果是,您会遇到什么错误?
  • 我使用了 ajax 但脚本没有显示任何内容,请帮助我..
  • 添加您在问题中尝试过的代码
  • 我添加了一个脚本并从我使用的选择选项中删除了 Onchange,但没有任何改变

标签: javascript php html jquery ajax


【解决方案1】:

您需要使用 jquery 的 onchange 事件并获取 select 的值,然后使用 ajax 将其传递给您的 php 页面。您需要在代码中进行的更改:

您的html代码:

  <!--remove onchange from here-->
  <select id ="city" name ="city">
     <!--same code -->
   </select>

您的 jquery 代码:

//onchange of select
$("#city").change(function() {
  //get select value
  var city = $(this).val();
   console.log(city)
  //ajax call 
  $.ajax({
     url: 'urlofphppage',
    type: 'POST',
    data: {
      'city': city
    },
     success: function(data) {
      alert('Data: ' + data);
      //add data which recieve from server to div
      $("#content").html(data);
    }

  });
});

您的 php 页面:

 if (isset($_POST['city'])){
        $listings = $_POST['city'];
   
    switch ($listings) {
        case 'California':
            $ship=$ship+5; 
            echo $ship;//will send back to ajax as response
            break;
        case 'Alabama':
            $ship=$ship+10;
             echo $ship; //will send back to ajax as response
            break;
        case 'Texas':
            $ship=$ship+15;
           echo $ship; //will send back to ajax as response
    }
  }

【讨论】:

  • 当您更改任何选项时,它会自动调用。您可以在选择中测试它更改选项并检查您的浏览器控制台,您将看到当前选择的选项的值。查看this 了解更多信息。
  • 可以通过添加
  • 你能详细说明一下吗?显示的另一个选择选项是什么意思?
  • 是的,因为在您的代码中,您有 2 个选择框,我认为 id 您在这里给出了相同的值,请更改第二个选择框的 id 并相应地更改 $("#city")...。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 2012-03-06
  • 1970-01-01
相关资源
最近更新 更多