我在表格上有这种情况:
<form id="boxselection" method="POST" action="select.php">
<label class="labels" for="boxtype">
<input type="radio" name="boxtype" value="3x80">
<img src="...">
</label>
<label class="labels" for="boxtype">
<input type="radio" name="boxtype" value="4x80">
<img src="...">
</label>
</form>
<script>
$('.labels').on('click',function() {
$('#boxselection').submit();
});
</script>
在Chrome,Firefox和IE中一切正常,在Safari中PHP脚本没有收到任何POST值。
可行的解决方法是以这种方式修改javascript:
<script>
$('.labels').on('click',function() {
setTimeout( function() {
$('#boxselection').submit();
}, 1000);
});
</script>
现在正在运作,我的问题是......为什么?
答案 0 :(得分:1)
首先,我认为您需要将id
值添加到单选按钮,以便标签与各自的单选按钮相关联。 for
属性将自身与表单元素的id
相关联。
<form id="boxselection" method="POST" action="select.php">
<label class="labels" for="checkbox1">
<input type="radio" name="boxtype" id="checkbox1" value="3x80">
<img src="...">
</label>
<label class="labels" for="checkbox2">
<input type="radio" name="boxtype" id="checkbox2" value="4x80">
<img src="...">
</label>
</form>
至于jQuery on.()
,setTimeout
之后最有效的原因是单击标签&#后,单选按钮实际上没有立即更改39;在页面上,即使现在它已与id
的单选按钮正确关联。
您希望将代理添加到单选按钮本身的change
事件中,当您单击标签按钮时,该事件也会起作用:
$('form#boxselection').delegate('change', 'input[type="radio"]', function(e) {
$('#boxselection').submit();
});
以上代码将触发表单中任何单选按钮的更改事件提交表单。