jQuery - 获取基于属性的选定内容后下拉列表中下一个选项的总值

时间:2015-03-09 10:57:38

标签: javascript jquery select option

HTML输出

<option data-task-hours="100" value="1"> - Parent Task</option>
<option data-task-hours="50" value="2"> -  - Child task</option>
<option data-task-hours="50" value="3"> -  - Child task</option>

jQuery Code获取下一个选项的值:

$('#dropDownId option:selected').next().data('task-hours');

尝试了多个子任务的循环,但它不起作用:

var foo = [];
$('#dropDownId :selected').each(function(i, selected){
  foo[i] = $(selected).data('task-hours');
});

如何在所选择的选项之后获取下一个选项的总值/组合值?

在上述情况下,总值应为50 (child 1) + 50 (child 2) = 100 (parent task),即子任务总值不应超过父任务的值

7 个答案:

答案 0 :(得分:0)

为什么不循环它。首先得到所选的值。然后运行如下的循环。

注意:下面不是无错误代码,它就像alogirthm或步骤。

var startfrom = $('#dropDownId option:selected').attr('value');

var alltext = "";
$('option').each(function(key,attr) {
{
if(key<startfrom) return;
alltext += attr.text;

}

console.log(alltext);

答案 1 :(得分:0)

$("#dropDownId").on('change', function () {
    var a=$('#dropDownId option:selected+option').attr('data-task-hours');
    var b=$("#dropDownId option:selected").attr('data-task-hours');
    var c=Number(a)+Number(b);
    alert(c);
});

<强> JS FIDDLE

答案 2 :(得分:0)

您可以使用:

 var sum=0;
 alert($('select option:first').attr('data-task-hours'))
 $('select option:gt(0)').each(function(){
  sum+= parseInt($(this).attr('data-task-hours'));
 });
 if($('select option:first').attr('data-task-hours')==sum){
  alert("parent and children have same hours");
}

Working Demo

答案 3 :(得分:0)

你是不是喜欢这样的?演示@ Fiddle

var sOpt = $("#sel option:selected");
var nextOpts = sOpt.nextAll();
var sum = 0;
nextOpts.each(function() {
    sum += $(this).data("taskHours");
});
if ( sum > sOpt.data("taskHours") ) {
    alert ("Total is greater than parent");
} else {
    alert (sum);
}

HTML:

<select id="sel">
    <option data-task-hours="100" value="1" selected="selected"> - Parent Task</option>
    <option data-task-hours="50" value="2"> -  - Child task</option>
    <option data-task-hours="50" value="3"> -  - Child task</option>
</select>

答案 4 :(得分:0)

nextAll()方法:

var selectedOption = $('#dropDownId option:selected')
var selectedOptionValue = selectedOption.data('task-hours');
var sum = 0;
selectedOption.nextAll().each(function(){
    if (sum < selectedOptionValue) {
        sum += $(this).data('task-hours');
    } else {
        sum = selectedOptionValue;
        return false;
    }
});

答案 5 :(得分:0)

这有效:

var indexOfSelected = $("#dropDownId option:selected").index()
 var options = $("#dropDownId option")

 var children = options.slice(indexOfSelected + 1, options.length)

 total = 0
 $.each(children, function(){
     total += $(this).data("task-hours")
 });

console.log("Total sum of children: " + total)

答案 6 :(得分:0)

试试这个Working Demo

$("#dropDownId").on('change', function () {
    var one=$('#dropDownId option:selected+option').attr('data-task-hours');
    var onetwo=$("#dropDownId option:selected").attr('data-task-hours');
    var onethree=Number(one)+Number(onetwo);
    console.log(onethree);
});