需要从可用选项中随机选择的jQuery标识符 [英] Need a jQuery randomly selected identifier from options available

查看:88
本文介绍了需要从可用选项中随机选择的jQuery标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的选择标签中的多个选项之一中随机选择一个ID或值.另外,我还在徘徊如何快速计数选择标签中的所有选项标签,以及如何有效地随机调用ID字段,因此如果单击刷新",它将仅选择另一个随机ID?这是为了在测试环境中自动为质量检查人员填写表格.

I want to randomly select an id or value from one of the many option items in my select tag. Also I was wandering how do I quickly count all the option tags in my select tag, and how can I randomly call an ID field efficiently so if I hit refresh, it will just pick another random id? This is for a testing environment to auto fill a form out for qa people.

示例

<select id="selectVendor">
 <option value="55" selected="selected">Company A</option>
 <option value="56">Company B</option>
 <option value="57">Company XYZCYK</option>
... </select>

最后,我想将所选ID的值放入表单字段中...最终,我想在页面首次加载时使用ajax用随机选择的标识符填充整个表单.

Finally I want to put the value of my selected id into my form field... Eventually I want to use ajax to populate my entire form with the randomly selected identifier when the page first loads. thx

<input type="text" id="txtCompany" value="empty" />

推荐答案

var options = $("#selectVendor > option");

var random = Math.floor(options.length * (Math.random() % 1));

options.attr('selected',false)
.eq(random).attr('selected',true);

在jsfiddle的示例是此处.

Sample at jsfiddle is here.

这篇关于需要从可用选项中随机选择的jQuery标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆