若字符串是某种东西,那个么值为null

If string is something then value is null

本文关键字:null 字符串      更新时间:2023-09-26

在javascript中,我如何编写一个函数,如果字符串为"Select client",则地址和联系电话字段应为null或空白。我已经有了这个功能,如果选择了客户名称,那么地址和联系电话字段将使用rails的populator gem自动输入。

 $('#client_id').change(function(){
        $.ajax({
            url: "/fetch_client",
            type: "POST",
            data: "id="+$(this).attr('value'),
            dataType: "json",
            success: function(data, textStatus, xhr){
                $('#client_address').attr('value',data.client_address);
                $('#client_contact_number').attr('value',data.client_contact_number);
            },
            error: function(xhr, textStatus, errorThrown){
            }
        });
  }); 

我是javascript新手,有人能帮我吗?

 $('#client_id').change(function(){
       if ($(this).val() == "Select client") {
                $('#client_address').attr('value', "");
                $('#client_contact_number').attr('value', "");
       } else {
        $.ajax({
            url: "/fetch_client",
            type: "POST",
            data: "id="+$(this).attr('value'),
            dataType: "json",
            success: function(data, textStatus, xhr){
                $('#client_address').attr('value',data.client_address);
                $('#client_contact_number').attr('value',data.client_contact_number);
            },
            error: function(xhr, textStatus, errorThrown){
            }
        });
     }
  });