尝试显示<tr>如果选择了一个选项

Trying to show a <tr> if an option is selected

本文关键字:选项 一个 如果 显示 lt tr gt 选择      更新时间:2023-09-26

我在表单中隐藏了一行,但我希望当我的选择选项设置为"取消"时显示这一行

我就是这样做的:

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>

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

$("#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

尝试

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();
}