简体   繁体   English

从菜单中下拉空选择

[英]Drop-down empty selection from the menu

I work with a JavaScript app and populate the drop-down menu with a JSON list. 我使用JavaScript应用程序,并使用JSON列表填充下拉菜单。

                        $("#address").append($('<option></option>').attr("value", "1").text(""));
                        $.each(wallets, function (index, wallet) {

                            if (selectedCurrency === wallet["currency"].toString().trim()) {
                                $("#address").append($('<option></option>').attr("value", "1").text(wallet["address"]));
                            }
                        })

The UI looks like this, 用户界面看起来像这样,

在此处输入图片说明

If I select the empty item (like in the picture), I would like to print empty in the console. 如果选择空白项(如图所示),则希望在控制台中打印empty Otherwise, I want to print the address value. 否则,我要打印地址值。 My intention is to write more code, but, this is where I would like to get started. 我的意图是编写更多代码,但是,这是我想要开始的地方。 The code I have, 我有的代码

$("#address").change(function () {

        if($(this).val()===""){
            console.log('empty');
        }

        else{
            console.log($(this).val());
        }
}

However, it only prints 1 in the console and can't distinguish between the empty bar and the address in concern. 但是,它仅在控制台中打印1,并且不能区分空白栏和相关地址。 If I use console.log("The value is " + $(this).text()); 如果我使用console.log("The value is " + $(this).text()); inside the change function, it prints every info all together in the console, change函数中,它将在控制台中一起打印所有信息,

The value is mp51mPC38Wtcmybdyd9MPEB2bKnw6eYbCsmqXjM7Mmg6B5LWMad7mHJi339ddaj7xXdBmvXxP1GmXXKojWQJKjgeoASnXVNXCS47z6n41jE2BKKpV6LsPb7dDdStjtuJf1FrYvMMmw1jtrWU5DADxvNR421MKFW1fposgzVMBymnzcZVF4jtZtGAggM5GuLog3Y5o52Mx4xMmq5Rgggfgy2TiRsvtcGm3rxx12R8XbYi9omkdt7ouyJnDXUp4LzdRSRP3ZhU57gUDKy6n2F2QEKk6Fqqk2yMTope5MYp1RtpT949kemrkdfp6qoVN3YiyJhq6nXPvgr3f7YpkS9j

The JSON data I have is something like, 我拥有的JSON数据类似,

[
  {
    "id": 1,
    "code": "BTC",
    "address": "mp51mPC38Wtcmybdyd9MPEB2bKnw6eYbCs",
    "currency": "Bitcoin"
  },
  {
    "id": 2,
    "code": "BTC",
    "address": "mqXjM7Mmg6B5LWMad7mHJi339ddaj7xXdB",
    "currency": "Bitcoin"
  },
  {
    "id": 4,
    "code": "BTC",
    "address": "mvXxP1GmXXKojWQJKjgeoASnXVNXCS47z6",
    "currency": "Bitcoin"
  }

   // some more data 
]

What the issue here? 这里有什么问题? My guess is I do something wrong to append the data to the drop-down list and needs to be changed. 我的猜测是我将数据附加到下拉列表中时做错了,需要更改。

I can provide more info if required .... 如果需要,我可以提供更多信息。

Just change the value 1 to "" in first option. 只需在第一个选项中将值1更改为""

$("#address").append($('<option></option>').attr("value", "").text(""));

$.each(wallets, function (index, wallet) {
    if (selectedCurrency === wallet["currency"].toString().trim()) {
          $("#address").append($('<option></option>').attr("value", "1").text(wallet["address"]));
      }
});

check this example. 检查这个例子。

 $("#address").append($('<option></option>').attr("value", "").text("")); $("#address").append($('<option></option>').attr("value", "1").text("123")); $("#address").append($('<option></option>').attr("value", "2").text("456")); $("#address").change(function () { if($(this).val()===""){ console.log('empty'); } else{ console.log($(this).val()); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="address"> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM