Javascript If语句在自动完成中不起作用

I have the following Javascript code that works perfectly as far as the autocompleting as well as filling 5 additional input fields, based on the loaded array from MySQL in the type_code.php file.

The sixth element of the array val(names[6]) is filled with a database value of either Yes or No and I am trying to use this value to control whether an additional form field becomes readonly or readwrite at the front end.

It seems as if my if statement is badly formatted, as if I run the two lines of codes without the if everything works as expected.

        // Autocomplete Invoice To from Code and sets ReadOnly/ReadWrite based on Editable field in Code record
        $('#add_at_inv2_code1').autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url : 'type_code.php',
                    dataType: "json", 
                    method: 'post',
                    data: {
                        name_startsWith: request.term,
                        type: 'code_table',
                        row_num : 1
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            var code = item.split("|");
                            return {
                                label: code[0],
                                value: code[0],
                                data : item
                            }
                        }));
                    }
                });

            },
            autoFocus: true,              
            minLength: 0,
            select: function( event, ui ) {
                var names = ui.item.data.split("|");                        
                $('#desc_at_inv2_code1').val(names[1]);
                $('#add_at_inv2_code2').val(names[2]);
                $('#add_at_inv2_client').val(names[3]);
                $('#add_at_salesrep').val(names[4]);
                $('#add_at_category').val(names[5]);

                // Code that is not working
                if($(val(names[6])) == "Yes") {
                $('#add_at_inv2_client').prop('readonly',false);
                $('#add_at_inv2_client').prop('disabled',false);
                } else {
                $('#add_at_inv2_client').prop('readonly',true);
                $('#add_at_inv2_client').prop('disabled',true);
                }

            }               
        });

Can someone please help me structure this if statement correctly?

Thanks, Adri

The val() method returns or sets the value attribute of the selected elements.

Note: The val() method is mostly used with HTML form elements.

Use

if(names[6] == "Yes") 

instead of

if($(val(names[6])) == "Yes")

=========================================================================

<script type="text/javascript">
    // Autocomplete Invoice To from Code and sets ReadOnly/ReadWrite based on Editable field in Code record
            $('#add_at_inv2_code1').autocomplete({
                source: function( request, response ) {
                    $.ajax({
                        url : 'type_code.php',
                        dataType: "json", 
                        method: 'post',
                        data: {
                            name_startsWith: request.term,
                            type: 'code_table',
                            row_num : 1
                        },
                        success: function( data ) {
                            response( $.map( data, function( item ) {
                                var code = item.split("|");
                                return {
                                    label: code[0],
                                    value: code[0],
                                    data : item
                                }
                            }));
                        }
                    });

                },
                autoFocus: true,              
                minLength: 0,
                select: function( event, ui ) {
                    var names = ui.item.data.split("|");                        
                    $('#desc_at_inv2_code1').val(names[1]);
                    $('#add_at_inv2_code2').val(names[2]);
                    $('#add_at_inv2_client').val(names[3]);
                    $('#add_at_salesrep').val(names[4]);
                    $('#add_at_category').val(names[5]);

                    // Code that is not working
                    if(names[6] == "Yes") {
                        $('#add_at_inv2_client').prop('readonly',false);
                        $('#add_at_inv2_client').prop('disabled',false);
                    } else {
                        $('#add_at_inv2_client').prop('readonly',true);
                        $('#add_at_inv2_client').prop('disabled',true);
                    }

                }               
            });
    </script>
Need following change in your code
// Code that is not working
if(names[6] == "Yes") //Change this if($(val(names[6])) == "Yes")
{
   $('#add_at_inv2_client').prop('readonly',false);
   $('#add_at_inv2_client').prop('disabled',false);
}
else
{
   $('#add_at_inv2_client').prop('readonly',true);
   $('#add_at_inv2_client').prop('disabled',true);
}