我有一个小型的网络应用程序,我正在测试,之前这个工作:
if ($(this).is(':radio') && $(this).is(':checked'))
{
val = $(this).val();
saveThis = true;
} else if ($(this).is(':checkbox') && $(this).is(':checked')) {
val = true;
saveThis = true;
} else if (($(this).is(':checkbox') || $(this).is(':radio'))) {
saveThis = false;
} else {
val = $(this).val();
saveThis = true;
}
以下是表格:
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Capture Type:</legend>
<input id="capture_type_onsite" name="capture_type" type="radio" value="onsite" />
<label for="capture_type_onsite" style="width: 200px;">OnSite</label>
<input id="capture_type_phone" name="capture_type" type="radio" value="phone" />
<label for="capture_type_phone" style="width: 200px;">Phone</label>
</fieldset>
</div>
但现在,当我console.log($(this))
($这是表单元素)时,我得到:
<input id="contact_type_member" name="contact_type" type="radio" value>
这是上面的代码,其中引用了一些console.log:
if ($(this).is(':radio') && $(this).is(':checked'))
{
console.log('Radio checked: '+$(this).attr('name')+' '+$(this).val());
console.log($(this));
val = $(this).val();
saveThis = true;
} else if ($(this).is(':checkbox') && $(this).is(':checked')) {
console.log('Checkbox checked: '+$(this).attr('name')+' '+$(this).val());
val = true;
saveThis = true;
} else if (($(this).is(':checkbox') || $(this).is(':radio'))) {
console.log('Checkbox or radio NOT checked: '+$(this).attr('name')+' '+$(this).val());
saveThis = false;
} else {
console.log('OTHER input: '+$(this).attr('name')+' '+$(this).val());
val = $(this).val();
saveThis = true;
}
这就是(:radio)是(:选中) - 首先是if语句 - 显示:
Radio checked: capture_type
[<input id="capture_type_onsite" name="capture_type" type="radio" value>]
我正在尝试使用localStorage保存(我创建了所有表单元素的对象,然后使用JSON.stringify保存它),然后根据他们选择的记录重新填充记录。在我实现jQuery Mobile之前,这一切都有效,所以我猜它必须对值进行一些操作,但我不确定。从调试语句中可以看出,到达此值时,该值为空。
提前致谢, 汉斯
答案 0 :(得分:4)
尝试:
var radio_val = $('input[name=capture_type]:checked').val();
alert('Radio Value: '+radio_val);