A function in jQuery passes the identifier of a form element as a parameter

I am trying to make a function in jQuery that will take the form identifier of the ID of the SELECT element where the dynamically created option will be displayed. But I have to repeat this work for other instances of the form. I created the following code, but it does not work.

JQuery Script

function getOffice(ID){
    $.post('dynamicOffice.php',{operator:$(this).val()},function(output){
    $('ID').html(output);
    });
        $('ID').removeAttr('disabled');
}

The main HTML file

     
<select id="senderOperator" name="senderOperator" tabindex="1" onchange=getOffice(sender)>
    <option value=""><--SELECT an Operator --></option>                 
        <?php getOption($operator,Operator) ?>
</select>

<select id="sender" name="sender" tabindex="1" disabled="disabled">
    <option value=""><--SELECT the Operator First --></option>
</select>

dynamicOffice.php

<?php
include('generateOption.php');
    $country=$_POST['operator'];
    $officeSql="SELECT * FROM myoffice WHERE Operator='$country'";
    getOption($officeSql,Name);
?>

generateFormElement.php

      
<?php
include("include/dbConnect.php");       

function getOption($rsSql,$colName){

    $sResult=mysql_query($rsSql) or die("Could Not Fetch Records");

    while ($s_Office = mysql_fetch_array($sResult))
    {
        echo("<option value='".$s_Office["$colName"]."'>".$s_Office["$colName"]."</option>");
    }
}
?>
+3
source share
1 answer

Remove the quotation marks around the parameter and connect #to its beginning.

$('#' + ID).html(output);

$('#' + ID).removeAttr('disabled');

Also, it thismost likely refers to windowinstead of what you expect, so the following will not work:

{operator:$(this).val()}

select, this :

onchange=getOffice(sender,this)

... :

function getOffice(ID, el){
    $.post('dynamicOffice.php',{operator:$(el).val()},function(output){
        $('#' + ID).html(output);
    });
    $('#' + ID).removeAttr('disabled');
}
+15

All Articles