我使用图像替换单选按钮,隐藏按钮,并根据单击的图像选择它们。
当一个图像被点击时,我希望他们选择一个单选按钮,然后提交。
这是我的原始代码:
$('#settings img').click(function() {
$(this).parent().find(":radio").attr('checked', true);
});如果我点击一张图片,然后提交,这是可行的。
我将其扩展为在单击时提交表单:
$('#settings img').click(function() {
$(this).parent().find(":radio").attr('checked', true);
$("#settings").submit();
});在这种情况下,表单会提交,但不会更改值。如果我通过单击按钮来提交它,它可以正常工作。
我哪里错了?
发布于 2013-05-17 22:18:32
这里的问题是赋予checked属性的true值。
jsfiddle在Chrome中提供了works。但是it 不能在火狐中使用。要使它也能在Firefox中工作,请使用checked属性的checked值。
$('#settings img').click(function() {
$(this).parent().find(":radio").attr('checked', 'checked');
});另一种方法是直接操作(以及) DOM元素的checked属性:
$(this).parent().find(":radio").get(0).checked = true;发布于 2012-10-23 21:45:45
Javascript是异步的。看起来表单在jQuery可以选中复选框之前提交。尝试将提交放在回调中:
$('#settings img').click(function() {
$(this).parent().find(":radio").attr('checked', true).one('change', function(){
$("#settings").submit();
});
});发布于 2012-10-23 21:56:48
也许不是最好的答案,但是如果你给你的图片一个类,你可以用.image替换:#settings img,然后它就可以工作了
https://stackoverflow.com/questions/13031724
复制相似问题