doucan4815 2017-12-12 06:32
浏览 98
已采纳

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

  • 写回答

2条回答 默认 最新

  • dongyue7796 2017-12-12 06:38
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败