I am using this method to answer a form randomly
$('table').filter(function () {
return ($(this).find('input').size() > 0);
}).find('tr').each(function () {
$(this).find('input').filter(function () {
return (Math.round(Math.random()) == 1);
}).each(function () {
var typ = this.type;
if (typ == 'checkbox' || typ == 'radio') {
this.checked = true;
}
if (typ == 'select' || typ == 'select-one') {
var value = $.map(this, function (elt, i) {
return $(elt).val();
}).sort()[0];
this.val(value);
}
if (typ = "text" || typ == "textarea") {
$(this).val(Math.floor(Math.random() * 11));
}
});
});
The input tag doesn´t have any
selecttype and you can´t set the selected value this waythis.val(value);as val() is a jQuery function, try$(this).val(value)(for inputs). You will have to change your slector to includeselectelements as well if you want to select on of its options.By using a single
=instead of==you are re declaring the variable making the conditiontrue.You should also be able to shorten the code by not using find() and each() so much. The following selector selectes
inputelements with intrelement within atableand then randomly filtering it as in your example.