试图显示是否选择了一个选项

时间:2013-08-16 00:46:25

标签: javascript jquery html drop-down-menu sharepoint-2010

我在表单中隐藏了一行,但是当我的选择选项设置为“已取消”时,我希望显示此行

这就是我的方式:

if($("option[value='Cancelled']").attr("selected") == "selected"){
    //Remove New option when Cancelled
    $("option[value='New']").remove();
    $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "Cancel Note"; }).closest("tr").show();
}

在控制台中,我注意到所选属性在切换时不会动态改变,我认为这是问题,但我不知道如何解决这个问题。

其余代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script><script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

//Disables unused Priority radiobox
function hideFields() {
    //priority - disable unchecked radiobox
    var reg = document.getElementById("ctl00_m_g_0537f4e9_69aa_409a_b5ed_15e3624efeab_ctl00_ctl05_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00");
    var high = document.getElementById("ctl00_m_g_0537f4e9_69aa_409a_b5ed_15e3624efeab_ctl00_ctl05_ctl02_ctl00_ctl00_ctl04_ctl00_ctl01");

    if(reg.checked == true){
        high.disabled=true;
    }else{
        reg.disabled=true;
    }
    //On item edit, set status to In Progress if in New
    if($("option[value='New']").attr("selected") == "selected"){
        //Disable New
        $("option[value='New']").remove();
        $("option[value='Completed']").remove();
        $("option[value='Cancelled']").remove();

        //Enable In Progress
        $("option[value='In Progress']").attr("selected","selected");

    }else if($("option[value='In Progress']").attr("selected") == "selected"){
        //Remove New option
        $("option[value='New']").remove();
        $("option[value='In Progress']").remove();
        $("option[value='Completed']").attr("selected","selected");
        $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "Cancel Note"; }).closest("tr").hide();
    }else if($("option[value='Completed']").attr("selected") == "selected" ){
        //Remove New option when Completed/Cancelled
        $("option[value='New']").remove();
        $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "Cancel Note"; }).closest("tr").hide();
    }else if($("option[value='Cancelled']").is(':selected')){
        //Remove New option when Cancelled
        $("option[value='New']").remove();
        $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "Cancel Note"; }).closest("tr").show();
}

    //call orderID
    orderID();
}
function PreSaveAction() {
    //Check is Work Order is claimed
    if ($('#content').length == 0){
    alert("Please claim Work Order by typing your name or email in NAPA User Field");
    return false ;
    }
    else { return true ;}
}

//I've deprecated this function - Not compatible with Chrome/FF
function findacontrol(FieldName) {
   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
   {
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }
   }
}

//Sets Order ID field to read only in edit form
function orderID() {
    //Set the Field to Read only and change its background colour
    $("input[title='Order ID']").attr("readonly","true").css('background-color','#F6F6F6');

    //call setFocus();
    setFocus();
    }

//Sets cursor Focus to NAPA users
function setFocus() {
    //set focus to NAPA user
    document.getElementById("ctl00_m_g_0537f4e9_69aa_409a_b5ed_15e3624efeab_ctl00_ctl05_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv").focus();
}

 </script>

2 个答案:

答案 0 :(得分:1)

我不知道是什么触发了您的代码块,但仅出于测试目的,我将在select上使用change事件。我就是这样做的:

$("#myselect").on("change", function () {
    var $this = $(this);
    if ($this.val() == "Cancelled") {
        //Remove New option when Cancelled
        $this.find("option[value='New']").remove();
        $("nobr").filter(function () {
            return $.trim(this.childNodes[0].nodeValue) === "Cancel Note";
        }).closest("tr").show();
    }
});

Fiddle

答案 1 :(得分:0)

尝试

if($("option[value='Cancelled']").is(':selected')){
    //Remove New option when Cancelled
    $("option[value='New']").remove();
    $("nobr").filter(function () { return $.trim(this.childNodes[0].nodeValue) === "Cancel Note"; }).closest("tr").show();
}