简体   繁体   English

添加选择下拉菜单的 rel 属性

[英]Add rel attribute of select dropdown

I have a form where I'd like to show the user a running total of the options they've selected.我有一个表单,我想向用户显示他们选择的选项的运行总数。 However, I need to use the value of the field in the PHP that processes the form (it sends an e-mail with what options the user selected from multiple dropdown menus).但是,我需要使用处理表单的 PHP 中字段的值(它发送一封电子邮件,其中包含用户从多个下拉菜单中选择的选项)。

Therefore, I'd like to put the numeric total in the 'rel' attribute and sum that instead.因此,我想将数字总数放在 'rel' 属性中并求和。 Here's an example:下面是一个例子:

<option value="1x VIP Ticket" id="one" rel="160">1x VIP Ticket</option>

I have used the following jquery code before to do this with checkboxes, is it possible to make it work with select menus?我之前使用以下 jquery 代码对复选框执行此操作,是否可以使其与选择菜单一起使用? I don't think there is, looking at http://api.jquery.com/category/selectors/form-selectors/ ...so perhaps a way to do this with vanilla JS?我不认为有,看看http://api.jquery.com/category/selectors/form-selectors/ ...所以也许是一种用香草 JS 做到这一点的方法?

$(document).ready(function() {
    function recalculate() {
        var sum = 0;

        $("input[type=checkbox]:checked").each(function() {
             sum += parseInt($(this).attr("rel"));
        });

        $("#output").html(sum);
    }

    $("input[type=checkbox]").change(function() {
        recalculate();
    });

});`

Here's the revised code to the use the data attribute:这是使用 data 属性的修改后的代码:

<script type="text/javascript"> <!--
$(document).ready(function() {
    function recalculate() {
        var sum = 0;

        $("#game1, #game2 option:selected").each(function() {
            sum += parseInt($(this).data("total"));
        });

        $("#output").html(sum);
    }

    $("#game1, #game2 option:selected").change(function() {
        recalculate();
    });

});

</script>

Tickets for Game 1
<select name="game1" id="game1">
<option value="1x VIP Ticket" id="1vip" data-total="160">1x VIP Ticket</option>
<option value="1x Regular Ticket" id="1regular" data-total="100">1x Regular Ticket</option>
</select>

Tickets for Game 2
<select name="game2" id="game2">
<option value="1x VIP Ticket" id="2vip" data-total="160">1x VIP Ticket</option>
<option value="1x Regular Ticket" id="2regular" data-total="100">1x Regular Ticket</option>
</select>


<div id="output"></div>

Options don't have a rel attribute.选项没有rel属性。 Try using a data attribute instead:尝试改用data属性:

<option value="1x VIP Ticket" id="one" data-total="160">1x VIP Ticket</option>

...

sum += parseInt($(this).data("total"));

EDIT: Based on your last attempt, try this:编辑:根据你上次的尝试,试试这个:

$(document).ready(function() {
    function recalculate() {
        var sum = 0;

        $("#game1, #game2").find("option:selected").each(function() {
            sum += parseInt($(this).data("total"));
        });

        $("#output").html(sum);
    }

    $("#game1, #game2").change(function() {
        recalculate();
    });

});
var selectedVal = $(this).find("option:selected").attr("rel");

选项 rel 属性值获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM