jQuery 与表单选择元素替换发生冲突的问题

发布于 2024-12-21 12:14:53 字数 603 浏览 0 评论 0原文

我正在为一个信息亭项目编写一个选择元素替换脚本,该脚本采用一个元素并替换它,它是带有 div 的 。我有两个版本 - 和非模态 来说,当它们的单击相应的 div.option。我相信与 jQuery 存在某种冲突导致了这个问题,但我太新手了,无法弄清楚。

在我的代码中,您会注意到添加 html 的许多变量和实例,这似乎是不必要的。该脚本的目的是替换由表单生成器生成的表单元素,表单生成器生成一个简单的标签并且无法更改。

我已在此处包含我的代码:http://jsfiddle.net/zumwalt/52At7/

非常感谢任何帮助!

I'm working on a select element replacement script for a kiosk project that takes a element and replaces it and it's <options>s with divs. I have two versions - 'normal' and 'modal' for both <select> and <select[multiple="multiple"]> elements. Independently of each other, the individual replacements work as they should. When I place all of them on a page, they break functionally. In particular, for both the non-modal <select> and non-modal <select[multiple="multiple"]> aren't toggling classes when their respective div.options are clicked. I believe there is some kind of conflict with the jQuery that's causing the problem, but I'm too much of a novice to figure it out.

In my code, you'll notice alot of variables and instances of adding html, which may seem unnecessary. The purpose of the script is to replace form elements being generated by a form builder which generates a simple tag and cannot be altered.

I've included my code here: http://jsfiddle.net/zumwalt/52At7/

Any help is greatly appreciated!

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

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

发布评论

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

评论(1

如何视而不见 2024-12-28 12:14:53

正在发生的事情是这样的:

 $('div.select-normal div.option-'+value+'').click(function() { 
     option.prop("selected", "selected");
     $('div.select-normal div.option').removeClass('selected');
     $(this).toggleClass('selected');
 });

当上面的代码为您的正常选择执行时,CSS 类被正确应用。然而,稍后您将获得模态选择多的代码:

$('div.option').click(function() {
    var value = $(this).attr('id');
    var option = $('option[value=' + value + ']');
    var optiontext = $(this).text();

    option.prop("selected", !option.prop("selected"));
    $(this).toggleClass('selected'); // HERE IS THE ERROR
    if ($(this).hasClass('selected')) {
        $('ul.multiselect-dropdown-options')
            .append('<li id="' + value 
                + '" class="dropdown-option drop-down-option-' 
                + value + '">' + optiontext 
                + '<a class="remove-option">x</a></li>');
    } else {
        $('.drop-down-option-' + value).fadeOut().remove();
    }
});

由于单击正常的选择多也会触发 $('div.option').click(),因此它正在执行toggleClass 并删除之前的“selected”类之前添加。

顺便说一句,如果你正在处理你的小提琴,那么调试它是非常困难的。当向 SO 发布小提琴时,请发布一个您不会编辑的分叉 URL。

更新

授人以鱼,可食一日……授人以鱼,可食一生。尽管你的小提琴正在改变,但我使用 Google Chrome 开发者工具对此进行了调试。只需转到您的小提琴,右键单击,然后选择“检查元素”。单击“脚本”选项卡,然后从下拉列表中选择“show/”脚本。然后,您可以单击行号以在 javascript 中设置断点。返回 Chrome,在结果窗格中执行某些操作,Chrome 应该会遇到断点。然后,您可以逐步执行操作并逐行查看脚本如何影响 DOM。

不过,您需要在each() 和click() 函数内部设置断点,因为Chrome 没有很好的Step Into 功能(它会步入jquery 源代码)。此外,拥有 2 个显示器显示在这里有很大帮助,这样您就可以在一个显示器上单步执行调试器,同时在另一个显示器上查看小提琴 DOM 结果。

Here is what is happening:

 $('div.select-normal div.option-'+value+'').click(function() { 
     option.prop("selected", "selected");
     $('div.select-normal div.option').removeClass('selected');
     $(this).toggleClass('selected');
 });

When the code above executes for your normal select many, the css class is correctly being applied. However, later on you have this code for the modal select many:

$('div.option').click(function() {
    var value = $(this).attr('id');
    var option = $('option[value=' + value + ']');
    var optiontext = $(this).text();

    option.prop("selected", !option.prop("selected"));
    $(this).toggleClass('selected'); // HERE IS THE ERROR
    if ($(this).hasClass('selected')) {
        $('ul.multiselect-dropdown-options')
            .append('<li id="' + value 
                + '" class="dropdown-option drop-down-option-' 
                + value + '">' + optiontext 
                + '<a class="remove-option">x</a></li>');
    } else {
        $('.drop-down-option-' + value).fadeOut().remove();
    }
});

Since clicking on the normal select many also triggers a $('div.option').click(), it is executing toggleClass and removing the 'selected' class that was added before.

BTW, it is very hard to debug your fiddle if you are working on it. When posting a fiddle to SO, please post a fork URL that you will not be editing.

Update

Give a man a fish and he eats for a day... teach a man to fish and he eats for a lifetime. Despite the fact that your fiddle was changing, I debugged this using the Google Chrome Developer tools. Just go to your fiddle, right click, and choose Inspect Element. Click the Scripts tab, and choose the "show/" script from the dropdown. You can then click on the line numbers to set breakpoints in the javascript. Go back to Chrome, do something in the Result pane, and Chrome should hit a breakpoint. You can then step through the actions and see, line by line, how the DOM is affected by your script.

You will need to set breakpoints inside of your each() and click() functions though, as Chrome doesn't have very good Step Into functionality (it steps into the jquery source). Also, having a 2-monitor display helps a LOT here, so that you can step through the debugger on one monitor while viewing the fiddle DOM result in another monitor.

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