JSON从数组到文本输入html获取唯一的随机值,并在显示所有值时显示文本

JSON从数组到文本输入html获取唯一的随机值,并在显示所有值时显示文本,html,jquery,arrays,json,random,Html,Jquery,Arrays,Json,Random,这是我的选项代码和文本框 <select id="sel" class="form-control input-lg" data-live-search="true"> <option value="">-- Select Your Country--</option> </select><br/><br/><br/> <input type = "text" id = "txtNa

这是我的选项代码和文本框

  <select id="sel" class="form-control input-lg" data-live-search="true">
    <option  value="">-- Select Your Country--</option>
    </select><br/><br/><br/>

<input type = "text" id = "txtName" class="form-control input-lg" />
</div>
我已经将JSON填充到下拉列表中,并显示到文本输入框中

$('#sel').append('<option value="' + value.coupon + '">' + value.country + '</option>');

        $('#sel').change(function () {
var str = $("#sel").val();
        $("#txtName").val(str);
}


一种方法是保持对列表firstCouponOptions的引用,如下面提到的代码所示,每次从firstCouponOptions中获取随机值时,将其显示在文本框中,同时从firstCouponOptions列表中删除该值。当它为空时,不显示优惠券消息

常量选项=[ {国家:第一,优惠券:[1,10,11]}, {国家:第二,优惠券:2}, {国家:第三,息票:3}, {国家:第四,优惠券:4}, {国家:第五,优惠券:5} ]; 函数getRandomValueFromListlist{ const randomeIndex=Math.floorMath.random*list.length; 返回列表[随机索引]; } $function{ const firstCouponOptions=选项[0]。优惠券; options.forEachvalue=>{ $'sel' .append+value.country+; }; $'sel'.changefunction{ 设str=$sel.val; 如果$sel选项:selected.text=='First'{ 如果firstCouponOptions.length{ str=getRandomValueFromListfirstCouponOptions; firstCouponOptions.patchefirstcouponoptions.indexOfstr,1; }否则{ str=无息票; } } console.logstr; $txtName.valstr; }; }; -选择你的国家-
谢谢你的回答。但我需要的是,当用户选择第一个时,它必须只向文本框返回一个随机值。我使用了你的原理,使用我的旧代码生成随机数,它可以工作。谢谢:我已经对它进行了标记并进行了投票。还有一件事,如何获得一个随机唯一值?号码不能重复。你们不必编辑答案,只要让我知道你们的评论,并看看那个链接;对于唯一值,您应该存储列表中最后输入的值…在将列表传递给getRandomValueFromList之前,请将其从列表中删除。您需要更新您的问题…无法添加注释。好的,非常感谢:
$('#sel').append('<option value="' + value.coupon + '">' + value.country + '</option>');

        $('#sel').change(function () {
var str = $("#sel").val();
        $("#txtName").val(str);
}

Array.prototype.random = function (length) {
       return this[Math.floor((Math.random()*length))];
 }
// var randomcoupon = value.coupon.random(value.coupon.length);