当文本框不为空或具有值时禁用下拉菜单

问题描述 投票:0回答:1

我在javascript中具有以下代码,此处的位置ID是我的UI中的文本框:

         var locationIdValue = $('[name="location_id"]').val();
         var chainIdSelection = $('[name="chain_id"]').val();
 if (chainIdSelection == "" && locationIdValue == "") {
    this.displayFailureMessage("Chain # is required.", 10000);
    canSearch = false;
 }

链ID是一个下拉列表,我具有下面的代码:

<select class="${prefix}ChainIdSelect" name="chain_id">
    <option value=''><fmt:message key="select"/></option>
    <c:forEach var="chainId" items="${ chainDescriptionList }" >
        <option value="${chainId.chainId}"> ${chainId.chainId} - ${ chainId.description }</option>
    </c:forEach>
</select>

链ID声明为:

           {mData    :"chain_id"
            ,sTitle   :'<fmt:message key="location.summary.chain_id"/>' 
            ,sClass   :"chainId"
            ,bSortable:true
            ,"filter" :{selector:".${prefix}ChainIdSelect"}
            }

当我的位置ID文本框具有某些值时,我正在尝试禁用链ID下拉列表。我尝试了以下解决方案:

         if(locationIdValue != "" || locationIdValue != null){
            document.getElementByName("chainid").disabled = true; 
        }

这没有用,到目前为止我尝试了很多其他事情。任何人都可以提出解决方案的建议。

javascript
1个回答
0
投票

不确定您的代码,但您可能想在事件回调中实现禁用逻辑。我想说的是添加回调,每次更改location_id时都会触发。

您的情况将是这样:

$('[name="location_id"]').change(function(){
    if(locationIdValue != "" || locationIdValue != null){
        $('[name="location_id"]').prop("disabled", true);
    }
});

请更改值后验证回调已执行!

© www.soinside.com 2019 - 2024. All rights reserved.