1

我有一个表格,为了测试,我想要一个按钮来自动填写每个组中的所有字段,对于复选框和收音机,只需从每个组类型交易中随机选择一个。大多数表格都非常大,出于测试目的,一直选择项目变得很痛苦。

表格的一个例子是:

<div class="rows"><b>Education</b><br>
<select name="edu" id="edu" validate="required:true"><option value="">Select One</option>
<option value="Some High School">Some High School</option>
<option value="High School graduate">High School graduate</option>
<option value="Some college">Some college</option>
<option value="College graduate">College graduate</option>
<option value="Master’s degree">Master’s degree</option>
<option value="Doctorate">Doctorate</option>
</select></div>

<div class="rows"><b>Payor status</b><br>
<select name="payor" id="payor" validate="required:true"><option value="">Select One</option>
<option value="Medicare/Medicaid">Medicare/Medicaid</option>
<option value="Insurance">Insurance</option>
<option value="Self Pay">Self Pay</option>
</select></div><br>

<div class="rows"><b>I am a physician.</b><br>
<input type="hidden" name="type33-8" value="YN">
<label><input type="radio" name="33-8" id="33Y" value="Yes" validate="required:true"> Yes</label> 
<label><input type="radio" name="33-8" id="33N" value="No"> No</label><br>
<label for="33-8" class="error">Please select an option</label></div><b>This doctor:</b><br>
<input type="hidden" name="type34" value="6PMultiple">

<div class="rows"><label>1.     Appears sincere.</label><br>
<input type="hidden" name="type34-247" value="6PMultiple">
<label><input type="radio" name="34-247" id="341" value="1" validate="required:true" > 1 - Very Strongly Disagree</label> 
<label><input type="radio" name="34-247" id="342" value="2" > 2 -  Strongly Disagree</label> 
<label><input type="radio" name="34-247" id="343" value="3" > 3 -  Disagree</label> 
<label><input type="radio" name="34-247" id="344" value="4" > 4 -  Agree</label> 
<label><input type="radio" name="34-247" id="345" value="5" > 5 -  Strongly Agree</label> 
<label><input type="radio" name="34-247" id="346" value="6" > 6 -  Very Strongly Agree</label></div>

<div class="rows"><label>2.     Is professional.</label><br>
<input type="hidden" name="type34-248" value="6PMultiple">
<label><input type="radio" name="34-248" id="341" value="1" validate="required:true" > 1 - Very Strongly Disagree</label> 
<label><input type="radio" name="34-248" id="342" value="2" > 2 -  Strongly Disagree</label> 
<label><input type="radio" name="34-248" id="343" value="3" > 3 -  Disagree</label> 
<label><input type="radio" name="34-248" id="344" value="4" > 4 -  Agree</label> 
<label><input type="radio" name="34-248" id="345" value="5" > 5 -  Strongly Agree</label> 
<label><input type="radio" name="34-248" id="346" value="6" > 6 -  Very Strongly Agree</label></div>

<div class="rows"><label>3.     Speaks clearly.</label><br>
<input type="hidden" name="type34-249" value="6PMultiple">
<label><input type="radio" name="34-249" id="341" value="1" validate="required:true" > 1 - Very Strongly Disagree</label> 
<label><input type="radio" name="34-249" id="342" value="2" > 2 -  Strongly Disagree</label> 
<label><input type="radio" name="34-249" id="343" value="3" > 3 -  Disagree</label> 
<label><input type="radio" name="34-249" id="344" value="4" > 4 -  Agree</label> 
<label><input type="radio" name="34-249" id="345" value="5" > 5 -  Strongly Agree</label> 
<label><input type="radio" name="34-249" id="346" value="6" > 6 -  Very Strongly Agree</label></div>

我曾想过遍历所有表单元素:

for ( var i=0; i < document.form1.elements.length; i++ ) {
    if ( document.form1.elements[i].type == "radio" ) {
        //Something
    }
}

我的问题是遍历每个元素,这将适用于选择下拉菜单(尽管不确定如何在那里随机选择一个下拉菜单选项),但这不适用于收音机的名称组。

我曾认为,为了选择下拉选项,我可以运行这个:

Math.random() * (max - min) + min

在获得选项计数以使用返回的整数随机选择一个之后,我不知道这是否可行,因为我没有测试它,因为我被困在无线电问题上。

4

3 回答 3

2

对于单选按钮,您可以遍历“行”div并从那里获取子单选按钮:

$("div.rows").each(function () {
    var radios = $(this).find("input[type=radio]");
    if (radios.length > 0) {
        var randomnumber = Math.floor(Math.random() * radios.length);
        radios[randomnumber].checked = true;
    }
});
于 2012-12-13T14:56:25.673 回答
1

不幸的是,jQuery 免费版本要复杂一些。例如,您可以将所有收音机组合在一起,然后从它们中随机选择。查看我在 JSFiddle 上的示例

这也是一个比 Reinder 更通用的版本。它适用于任何类型的 HTML。

JS代码:

var radios = {};
var radionames = [];

var tmpname;
for ( var i=0; i < document.form1.elements.length; i++ ) {
    if ( document.form1.elements[i].type == "radio" ) {
        tmpname = document.form1.elements[i].name;
        if (! radios[tmpname]) {
            radios[tmpname] = [];
            radionames.push(tmpname);
        }
        radios[tmpname].push(i);
    }
}

var idx; 
for (var i=0; i < radionames.length; i++) {
    tmpname = radionames[i];
    idx = Math.floor(Math.random()*(radios[tmpname].length - 1));
    document.form1.elements[radios[tmpname][idx]].checked=true;
}

​附带说明,您的 HTML 无效,元素 ID 必须始终是唯一标识符。

于 2012-12-13T15:07:56.477 回答
0

这是我为你想出的一些 jQuery:

$(function() {
    var radio_questions = $('div.rows');
    radio_questions.each(function() {
        var num_choices = $(this).find('input').length;
        var rand = getRandomIndex(1, num_choices - 1);
        $(this).find('input[value="' + rand + '"]').attr('checked', 'checked');
    });
});

function getRandomIndex(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

示例:http: //jsfiddle.net/w3GdT/7/

于 2012-12-13T15:06:47.570 回答