Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery.trigger(“单击”)不遵守event.preventDefault()_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript jquery.trigger(“单击”)不遵守event.preventDefault()

Javascript jquery.trigger(“单击”)不遵守event.preventDefault(),javascript,jquery,checkbox,Javascript,Jquery,Checkbox,“我的页面”上的表单中有复选框: <form id="oneform" action="blah.."> <input type="checkbox" name="checkboxes" id="chkbox1"/>Checkbox1 <input type="checkbox" name="checkboxes" id="chkbox2"/>Checkbox2 ... e.preventDefault在上述功能中的作用是:如果选中了复选框并

“我的页面”上的表单中有复选框:

<form id="oneform" action="blah..">
    <input type="checkbox" name="checkboxes" id="chkbox1"/>Checkbox1
    <input type="checkbox" name="checkboxes" id="chkbox2"/>Checkbox2
...
e.preventDefault在上述功能中的作用是:如果选中了复选框并单击了复选框,则prevent default将确保其保持选中状态,以便浏览器在提交表单之前记录复选框状态。因此,表单提交后,复选框将按预期取消选中,在返回按钮上,我将再次看到复选框被选中。这对我来说就像预期的一样

然后我在表单上有一个div,它显示元素列表,显示选中复选框的名称

<div>
  <% This is a JSTL code: for each element  %>
      <a class="close-tag" href="#" value="<% JSTL put the checkbox id %>"><span><% jSTL put checkbox name</span> X</a>
  <% end %>
</div>
})

我没有补充什么吗?它在FF、Chrome上不起作用 谢谢你的指点

更新 通过将$selector.trigger替换为vanilla JS alternative:document.getElementByIdselector.click…我能够解决这个问题。 在FF、IE8/9、safari、chrome、opera上都能如期工作

更新:

事实上,我错过了一些非常明显的事情。当您调用触发器时,您需要指定要触发的事件,因此它需要是触发器“单击”

如果我读对了,您是说它仍然显示为选中,但您希望它向服务器报告为未选中

那不太可能。一个元素没有两个这样的状态,只有一个


不过,您可以将复选框更改为未选中,提交表单,然后再次将其标记为已选中。

感谢您的回复。。。当我通过单击复选框来选中复选框时,我看到的是一系列事件:选中复选框->单击->复选框仍选中->表单提交-->页面重新加载->复选框现在未选中。现在如前所述,当我通过jquery.trigger假装点击操作时,流程是:选中复选框->使用触发器假装点击->复选框仍然选中->表单提交->页面重新加载->复选框仍然选中预期复选框我相信当jquery.trigger假装点击时出错了…可能是事件。默认值不是我在Firebug中加入了一些调试点,但我看不出有什么不同,我对我的帖子做了一个小的更新。注意到您没有将事件传递到触发器,这可能是问题所在。感谢您指出它…在问题中我忘记了…但在我的代码中…它就在那里…再次感谢!
<div>
  <% This is a JSTL code: for each element  %>
      <a class="close-tag" href="#" value="<% JSTL put the checkbox id %>"><span><% jSTL put checkbox name</span> X</a>
  <% end %>
</div>
$(".close-tag").bind('click', function(e) {
 e.preventDefault() // again for same reasons as mentioned above

// get the name of checkbox id from event target
.....
 var selector = ... // checkbox id 

// fake the checkbox clicking event
$("#"+selector).trigger("click");
// doing the above goes to the above jquery snippet as you would expect.

// the checkbox also remains checked as expected due to e.preventDefault()
// However on server side it should send the checkbox as "unchecked" instead it sends as  checked