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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!