1

我有一个简单的组合框,我可以在 JavaScript 中获得它的值。我正在这样做,所以我不需要刷新页面。另外我想在组合框之后使用选定的值进行一些条件分支,那么如何将组合框的值从 JavaScript 获取到我的变量$change

echo '<select id="combo_1">';
echo '<option value="2">Submative</option>';
echo '<option value="1">formative</option>';
echo '</select>';

下面是我的 JavaScript:

    <script type="text/javascript">
    $(document).ready(function() {

        $('#combo_1').change(function(){

        });

    });

    </script>

在这里我想做$change = $(this).val(),但显然我不能这样做。
有什么建议么?我想在同一页面上执行此操作而不刷新或不提交我的网址有点像这样

 http://localhost/lms/grade/report/userdef/index.php

我希望它在单击动作上,因为这取决于组合框 2 的选择,将调用非常不同的程序

4

3 回答 3

2

You're gonna want to use AJAX and submit the form, then you can access the returned data without ever refreshing the page.

Basically:

HTML

<select name="combo" id="combo_1">
    <option value="2">Submative</option>
    <option value="1">formative</option>
</select>

JavaScript

$('#combo_1').change(function() {
    $.post('calcScript.php', $(this).serialize(), function(data) {
        alert(data);
    });
});

in PHP, you can access your combo data via $_POST['combo'].

于 2013-07-02T11:06:16.217 回答
2
<script type="text/javascript">
 $(document).ready(function() {

  $('#combo_1').change(function(){
        var combo_1 = $(this).val();
        $.ajax({
                type: 'GET',
                url: 'ajax.php',
                data: {'combo_1':combo_1},
                success: function(data){
                    alert(data)
                }
            });
  });

 });

 </script>

ajax.php


 if( isset($_GET['combo_1]) ) {
    echo $change = $_GET['combo_1'];
 }
于 2013-07-02T11:12:17.327 回答
0

JS:

$.ajax({
    type: "POST",
url: './filename.php',
    beforeSend: function(){
        //If you want to do something 
},
data: 'data='$('#combo_1').val(), //Optional '&data2='+value+'&datan='+value,
    success: function(msg){
        alert(msg);
    }
});

PHP:

$val = $_POST['data'];
return 'received successfully';

这将提示“已成功接收”

于 2013-07-02T11:15:00.903 回答