Jquery - 选择后更改选项值

时间:2016-09-28 01:12:29

标签: php jquery ajax

如何使用jquery Ajax从选定的<option>显示不同的checkbox 文本。在我的表单上,我有一个两个复选框供用户选择价格范围。示例如下,

<input type="checkbox" name="price_range" value="1" id="ceo"> < RM 10,000 
<input type="checkbox" name="price_range" value="2" id="bod"> < RM 200,000

如果用户选择value='1'value='2',则两个选择选项值的输出将根据所选复选框进行更改。

//display application name
<select name="submited_by" id="submited_by">
  <option value="0">Choose Price Range</option>
</select>

//display name to approve application
<select name="hod" id="hod">
  <option value="0">Choose Price Range</option>
</select>

执行更改的Ajax

$("input[name='price_range']").click(function() {
    var price = $(this).val();

    $.ajax({
        type: 'POST',
        url: 'DropdownValuesForm.php',
        data: { price : price }, 
        success: function(data){
            $('#submited_by').html(data)
        }
    }); 

});

DropdownValuesForm.php

<?php
require 'db_connection.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $price = $_POST['price'];
}

if($price == '< RM 10,000'){ 
    //do the Query

    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_assoc($result)){
            $submited_by = $row['staff_id'];    
            $nama = $row['nama'];

            echo '<option value=" ' . $submited_by . ' ">' . $nama . '</option>';
        }   
    } 
    mysqli_free_result($result);

} else {
    //do the Query

    if(mysqli_num_rows($result) > 0){
        while($row=mysqli_fetch_assoc($result)){
            $submited_id = $row['staff_id'];
            $nama_pemohon = $row['nama'];

            echo '<option value=" ' . $submited_id . ' ">' . $nama_pemohon . '</option>';
        }   
    } 
    mysqli_free_result($result);
}
?>

就我而言,它仅会更改为<select name="submited_by">。我是否需要为<select name="hod" id="hod">创建另一个ajax函数,并创建另一个页面以从DropdownValuesForm.php等数据库中获取值。

1 个答案:

答案 0 :(得分:1)

要做你想做的事,你真的只需要坚持一个将发送指令的ajax页面。请参阅我的回答(下半部分在编辑/只是作为一个侧面here,以了解如何实现一个简单的调度程序。使用该帖子中的调度程序,以下是此工作流程的样子:

表单复选框:

<!-- Unless you need the form to submit a 1 or 2, it would be better to send 10,000 or 20,000 instead of 1 or 2 -->
<input type="checkbox" name="price_range" value="1" id="ceo" data-instructions='{"action":"get_price_range","value":"< RM 10,000","sendto":"#submited_by"}' class="ajax_trigger" />
<input type="checkbox" name="price_range" value="2" id="bod" data-instructions='{"action":"get_price_range","value":"< RM 20,000","sendto":"#hod"}' class="ajax_trigger" />

<强> jQuery的:

$(document).ready(function() {
    var doc = $(this);
    // Create ajax instance (from other post, see that script)
    var Ajax = new AjaxEngine($);
    // If you trigger on a class, you can do ajax anywhere on your page
    doc.on('click', '.ajax_trigger', function() {
        // You will likely want to check first that the click is enabling
        // the checkbox or disabling it (check/uncheck) before you run the ajax
        // Extract the instructions
        var dataInstr = $(this).data('instructions');
        // Send the instructions
        Ajax.send(dataInstr,function(response) {
            // Use the extracted sendto to save to a spot on this page
            // It would be better to return json string to decode. This way
            // you can return instructions along with the html for further
            // dynamic processing...but this instance, this should do
            $(dataInstr.sendto).html(response);
        });
    });
});

<强> $ _ POST:

您的帖子随后会发送:

Array(
    [action] => get_price_range
    [value] => < RM 10,000
    [sendto] => #submited_by
)

<强> XML:

使用其他帖子作为指南,您可以创建一个xml来将您的调度指向该位置:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <ajax>
        <action>
            <get_price_range>
                <include>/actions/get_price_range.php</include>
            </get_price_range>
        </action>
    </ajax>
</config>

然后,您的调度将此页面包含在呼叫中:

/actions/get_price_range.php

<强> /functions/getPriceOptions.php

将关键脚本放入函数(类/方法以获得更大的灵活性),以实现可移植性和重用。

<?php
/*
** @description  This will return your options
** @param $key [string] This is whatever you used to isolate the data in your sql call
** @param $db [resource] This is the mysqli connection that you need to run the query
*/
function getPriceOptions($key,$db)
    {
        ##################
        ## do the query ##
        ##################

        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_assoc($result)){
                $submited_by = $row['staff_id'];    
                $nama        = $row['nama'];
                # You may want to just return data and not a view
                # the function will be more flexible that way
                $disp[]      = '<option value=" ' . $submited_by . ' ">' . $nama . '</option>';
            }
        }
        mysqli_free_result($result);
        return (!empty($disp))? implode(PHP_EOL,$disp):'';
    }

<强> /actions/get_price_range.php

<?php
require_once(__DIR__.'/db_connection.php');
# include the function
require_once(__DIR__.'/functions/getPriceOptions.php');
# Isolate the post
$price = $_POST['price'];
# Get the query key word/value to search your database
$key   = ($price == '< RM 10,000')? 'whatever' : 'otherwhatever';
# Write to the view
echo getPriceOptions($key,$db);

<强>样本:

以下是链接帖子和上面的所有集成脚本的演示。这个例子略有不同,因为我没有你的表,我想发回post数组,所以你也可以看到它,但最终结果是一样的:

http://www.nubersoft.com/tester.php?example=39736539