需要根据条件隐藏选定的元素

时间:2013-08-23 15:52:04

标签: javascript jquery jquery-selectors global-variables variable-declaration

我有各种选项的下拉菜单

  • 正在进行中
  • 未订购
  • 已完成
  • 取消

我有一个表单,我需要在状态为新时隐藏ETA行,然后为任何其他状态启用它。这就是我试过的:

var progress, ordered, unordered, completed, cancelled, reg, high, user, trimUser, etaDate, etaRow, etaHour, etaMinutes;

function initiate() {
    //declaring necessary variables
    declaration()
}

function declaration(){
    //Declaration of variables for status changes
    sNew = $("option[value='New']");
    progress = $("option[value='In Progress']");
    ordered = $("option[value='Ordered']");
    unordered = $("option[value='Unordered']");
    completed = $("option[value='Completed']");
    cancelled = $("option[value='Cancelled']");

    //Declaring variables for priority changes
    reg = $('input[value="ctl00"]');
    high = $('input[value="ctl01"]');

    //Declaring current user using SPServices 
    user = $().SPServices.SPGetCurrentUser({
    fieldName: "Name",
    debug: false
    });

    //Declaring variables for ETA field manipulation
    etaRow = $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "ETA"; }).closest("tr");
    etaDate = $('input[title="ETA"]');
    etaHour = $("[id$=DateTimeFieldDateHours]");
    etaMinutes = $("[id$=DateTimeFieldDateMinutes]");

}

function hideETA(){
    //hide ETA options
    etaRow.hide();

    if(sNew.not(:checked)){
        alert('CP1');
        eta.show();
    }
}

所以我尝试这样做的方式是最初隐藏它(因为new是默认值),然后当它是新的时候,显示它。

修改 我想我已经解决了,这是我在@Richard和if语句和选择器的帮助下做到的:

function setETA(){
    //Hide, Display and Disable ETA based on stages         
    if(sNew.is(":checked")){
        etaRow.hide();
    }else if (progress.is(":checked")){
        //do nothing, should be already showing
    }else{
        etaDate.attr("readonly","true").css('background-color','#F6F6F6');
        etaHour.attr("disabled","disabled");
        etaMinutes.attr("disabled","disabled");
    }
}

1 个答案:

答案 0 :(得分:0)

2可能性:

  • 显示=无
  • 删除选项

对于显示方法:http://jsfiddle.net/r7gLU/1/

// Hide the Option
var opt = $('option[value=New]');
opt.css('display','none');
if( opt.parent().val() == 'New' )
    opt.next().attr("selected", "selected");

// Show the Option
var opt = $('option[value=New]');
opt.css('display','');