ajax 调用复选框 Internet Explorer

发布于 2024-12-22 00:47:11 字数 772 浏览 0 评论 0原文

我在使用 Internet Explorer 和 Ajax 时遇到了这个小问题。 所以首先我只使用了 php,一切都工作了,但是因为我不想重新加载页面,所以我使用了 ajax。

所以我有一个带有复选框的表单。当有人单击该复选框时,我的 ajaex 会被调用,并且数据库中的输入会发生更改。在 Firefox 中没有问题,但在 Internet Explorer 中不起作用。

这是我的代码的一部分:

<script language="javascript" type="text/javascript">
function changefield($doss, $display){
    $.get("update.php",{dossier: $doss, CSQ_DISPLAY:$display});
    alert("test");
}
</script>


echo '<form id="'.$r ['BC_DOSSIER'].'" method="get" action="">
<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">
</form>'; 

似乎在资源管理器中,我仅在选中复选框时才会收到警报。 (问题是因为它首先读取数据库是否必须检查,因此您可以稍后更改它)。

有人知道我哪里错了吗?

预先非常感谢您的答复。

I'm having this little problem with internet explorer and ajax.
So first I used just php, and everything, worked, but because I don't want to reload the page, I use ajax.

So I have a form with a checkbox. When someone clicks on the checkbox, my ajaex is called and the input is changed in the db. In firefox there is no problem, but It doesn't work in internet explorer.

Here's a part of my code:

<script language="javascript" type="text/javascript">
function changefield($doss, $display){
    $.get("update.php",{dossier: $doss, CSQ_DISPLAY:$display});
    alert("test");
}
</script>


echo '<form id="'.$r ['BC_DOSSIER'].'" method="get" action="">
<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">
</form>'; 

It seems that in explorer, I only get the alert when the checkbox was checked. (Problem because it first reads the db if it must be checked or not, so you can change it later).

Does someone know where I went wrong?

Thank you very much in advance for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-12-29 00:47:11

我更喜欢这样定义它,我讨厌在 HTML 中使用 onclick

edit FIXED (注册为 change 而不是点击
编辑 包裹在 $(document).ready()
编辑还添加了一个click事件

<?php
  echo '<input class="ajax_check" id="check_'.htmlspecialchars($r['BC_DOSSIER']).'" type="checkbox" name="CSQ_DISPLAY" '.$checked .' />';
?>
<script type="text/javascript">
  $(document).ready(function() {
    $('input.ajax_check').click(function() {
      this.blur();
    });
    $('input.ajax_check').change(function() {
      var dossierId = this.id.slice(6);
      var isChecked = (this.checked) ? 1 : 0; // better to explicitly convert bool to int for HTTP requests
      $.get('update.php',{
        dossier: dossierId,
        CSQ_DISPLAY: isChecked
      });
      alert('test');
    });
  });
</script>

这将将该处理程序注册到类名“ajax_check”的所有输入,而不会留下一个函数弄乱窗口对象,也不会弄乱你的 HTML。尝试一下,看看它是否解决了问题 - 它可能不是因为它基本上做同样的事情,但恕我直言,这是一种更好的方法。如果仍有问题,请回来找我,我们将进行调试。

请注意,使用这种方法时,

I would prefer to define it like this, I hate using onclick in my HTML:

edit FIXED (registered to change instead of click)
edit Wrapped in $(document).ready()
edit Added a click event as well

<?php
  echo '<input class="ajax_check" id="check_'.htmlspecialchars($r['BC_DOSSIER']).'" type="checkbox" name="CSQ_DISPLAY" '.$checked .' />';
?>
<script type="text/javascript">
  $(document).ready(function() {
    $('input.ajax_check').click(function() {
      this.blur();
    });
    $('input.ajax_check').change(function() {
      var dossierId = this.id.slice(6);
      var isChecked = (this.checked) ? 1 : 0; // better to explicitly convert bool to int for HTTP requests
      $.get('update.php',{
        dossier: dossierId,
        CSQ_DISPLAY: isChecked
      });
      alert('test');
    });
  });
</script>

This will register that handler to all inputs with the className 'ajax_check', without leaving a function cluttering up the window object, and without messing up your HTML. Try it out and see if it fixes the problem - it may not as it does basically the same thing, but it's a better way of doing it IMHO. If you still have a problem, come back to me and we'll debug it.

Note that using this approach, it is important that the <script> is executed after the DOM is ready, so it should either be defined in the body after the checkbox or (better) wrapped inside $(window).load() or (best) $(document).ready().

萧瑟寒风 2024-12-29 00:47:11

在您的代码中,后备函数值不会传递给 IE。

<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">

即)在上面的代码中 onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)" this.checked 对 IE 不起作用,所以你可以传递 this 并获取值在四个功能中,这适用于所有浏览器。例如)像这样

<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this)">

并获取函数内的选中属性。

我希望这会起作用。

In your code fallback function value wont passed for IE.

<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)">

i.e) in above code onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this.checked)" this.checked will not work for IE, so you can pass as this and get the value in four function, this will work for all the browsers. e.g) like this

<input type="checkbox" name="CSQ_DISPLAY" '.$checked .' onchange="changefield(\''.$r ['BC_DOSSIER'].'\',this)">

and get the checked attribute inside the function.

I hope this will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文