添加/删除选择框不提供值 [英] Add/Remove Select Box Not Giving Values

查看:158
本文介绍了添加/删除选择框不提供值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(问题在这篇文章的底部)

(The question is at the bottom of this post)

您好,我遵循这个教程

我已经做了3个盒子,1所有的值和2分开的值。

I have made 3 boxes total, 1 with all the values and 2 to separate the values.

如下图所示:

这是我的Jquery:

This is my Jquery :

<script type="text/javascript">
        $(document).ready(function() {
        $("#and").click(function() {
            $("#totalselect option:selected").each(function() {
            $("#select-and").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
            });
        });
        $("#removeand").click(function() {
            $("#select-and option:selected").each(function() {
            $("#totalselect").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
            });
        });
        $("#or").click(function() {
            $("#totalselect option:selected").each(function() {
            $("#select-or").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
            });
        });
        $("#removeor").click(function() {
            $("#select-or option:selected").each(function() {
            $("#totalselect").append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option>");
            $(this).remove();
            });
        });
        });
    </script>

这是我的HTML:

<div id="mass_options">
        <a href="JavaScript:void(0);" id="and">And</a>
        <a href="JavaScript:void(0);" id="or">Or</a>
        <select name="totalselect[]" id="totalselect" multiple size="5">
            <?php foreach ($reasons as $r) { ?>
                <option value="reason_<?php echo $r['reason_id'] ?>">Reason: <?php echo $r['reason'] ?></option>
            <?php } ?>
            <?php foreach ($options5 as $key => $value) { ?>
                <option value="status_<?php echo $key ?>">Session Status: <?php echo $value ?></option>
            <?php } ?>
            <?php foreach ($info as $key => $value) { ?>
                <option value="action_<?php echo $key ?>">Action: <?php echo $value ?></option>
            <?php } ?>
        </select>
        </div>

        <select name="and" id="select-and" multiple size="5" class="selectnew">
        </select>
        <select name="or" id="select-or" multiple size="5" class="selectnew">
        </select>
        <br>
        <br>
        <a href="JavaScript:void(0);" id="removeand">Remove And</a>
        <a href="JavaScript:void(0);" id="removeor">Remove Or</a>

问题

当我单击下一步它发送到一个页面,只是做一个var_dump的所有后的值,我注意到的是什么我添加到和框或或框不是在var_dump作为值。我可以得到它提交(我点击下一步)的唯一方法实际上是突出显示在和框或或框中的值。我怎么能这样,如果它在和框或或框,它是自动选择我提交到我的脚本,而无需我强调他们?

When I click Next it sends me to a page that just does a var_dump of all the post values, what I am noticing is what ever I add to the "and box" or the "or box" is not in the var_dump as a value. The only way I can get it to be submitted (when I click next) is actually highlighting the value in the "and box" or in the "or box". How can I have it so, if it is in either the "and box" or the "or box" that it is automatically selected for me to submit to my script without me having to highlight them?

我这样做的原因是,我可以动态建立一个查询,其中Where子句是AND或OR,所以我可以使电子邮件学生更多的动态,而不是只是切割和干燥。

The reason why I am doing it like this is so I can dynamically build a query with Where clauses being "AND" or "OR" so I can make emailing students a lot more dynamic rather then just cut and dry.

任何帮助将是巨大的。

推荐答案

提交你必须突出你的选择。你知道只有选定的选项会在帖子中发送。您可以在发送数据之前使用jQuery选择它们:

In a native form submit you have to "highlight" your options. You know only selected options will be send in the post. You can select them with jQuery before sending data:

$("form *:submit").click(function() { /* choose a better selector for submit button */
    $("#select-and options, #select-or options").attr("selected", true);
    $(this).parents("form:first").submit();
});

另一个没有autoselect的替代方法是使用AJAX发送帖子。

Another alternative without "autoselect" is to send the post with AJAX. In this way you can prepare your sending data.

..或编辑您的jQuery到此:

..or edit your jQuery to this:

<script type="text/javascript">
    $(document).ready(function() {
        $("#and").click(function() {
            $("#totalselect option:selected").appendTo("#select-and");
        });
        $("#or").click(function() {
            $("#totalselect option:selected").appendTo("#select-or");
        });
        $("#removeand").click(function() {
            $("#select-and option:selected")
                .removeAttr("selected")
                .appendTo("#totalselect");
        });
        $("#removeor").click(function() {
            $("#select-or option:selected")
                .removeAttr("selected")
                .appendTo("#totalselect");
        });
    });
    </script>

这篇关于添加/删除选择框不提供值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆