Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - Change Ajax Post parameters & returned HTML based on alternating dependant dropdowns

I have 3 dropdowns containing values that are populated on page load

<select class='form-control' id='make' placeholder='Make:'>
<select class='form-control' id='model' placeholder='Model:'>
<select class='form-control' id='version' placeholder='Version:'>

I have a function that updates the values in the 'other' dropdowns that aren't clicked, based on the value of the dropdown that is clicked - but I have this function repeated 3 times, for each dropdown

$('#model').change(function(){
    let selectedModel = $(this).val();
    $.ajax({
        url: 'php/dropdown.php',
        type: 'POST',
        data: {model: selectedModel},
        success:function(data)
        {  $('#make').html('');
           $('#version').html('');
            let makeJSON = JSON.parse(data)[0];
            let versionJSON = JSON.parse(data)[2];

            for (let i = 0; i < makeJSON.length; i++) {
                if (makeJSON[i].mMake!= '' && makeJSON[i].mMake!= null) {
                    $('#make').html($('#make').html() + '<option value="' + makeJSON[i].mMake + '">' + makeJSON[i].mMake + '</option>');
                }
            }

            for (let i = 0; i < versionJSON.length; i++) {
                if (versionJSON[i].mVersion != '' && versionJSON[i].mVersion != null) {
                    $('#version').html($('#version').html() + '<option value="' + versionJSON[i].mVersion + '">' + versionJSON[i].mVersion + '</option>');
                }
            }

        }
    });
});

And the PHP looks something like this:

$model = $_REQUEST['model'];

$sqlupdateModel = "SELECT DISTINCT mMake, mVersion FROM Cars WHERE mModel = '$model';
$stmtModel = sqlsrv_query( $conn, $sqlupdateModel);
if( $stmtModel === false)
{
    die( print_r( sqlsrv_errors(), true));
}
$updateModel = [];
while( $row = sqlsrv_fetch_array( $stmtModel, SQLSRV_FETCH_ASSOC)){
    $updateModel[] = $row;
}
echo json_encode(array($updateMake, $updateModel, $updateVersion));

...and this all works fine, Basically, I'm looking for a simpler solution for reusing the function (both JS & PHP) instead of rewriting it 3 times!

In terms of what I have attempted,

$('#make, #model, #version').change(function(){
let columnValue = $(this).val();
.......
data: {model: columnValue},
success:function(data)
{$(this).html(''); //this doesn't work obviously!

After this I'm snookered

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This one should work for JS side, you will have to check mapping function for response

$('#make, #model, #version').change(function(ev){
    let selected = $(this).val();
    let id = ev.target.id;
    let data = {};
    data[id] = selected;
    $.ajax({
        url: 'php/dropdown.php',
        type: 'POST',
        data: data,
        success:function(data)
        {  
           let options = ['make', 'model', 'version']
           const response = {
             make: JSON.parse(data[0].map(make => make.mMake)),
             model: JSON.parse(data[1].map(make => make.mModel)),
             version: JSON.parse(data[2].map(make => make.mVersion))
           }

           options.filter(option => option !== id).forEach(option => setDropdown(option, response[option]));
        }
    });
});
function setDropdown(id, data) {
    const id = `#${id}`
    $(id).html('');
    for (let i = 0; i < data.length; i++) {
        if (data[i] != '' && data[i] != null) {
                $(id).html($(id).html() + '<option value="' + data[i] + '">' + data[i] + '</option>');
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...