首页 > 解决方案 > 如果选中复选框,用户必须输入一个值

问题描述

我有以下输入字段和一个复选框:-

<input id="ClientManagerApproval_e565da24-d454-4537-b902-771a37689e9d_MultiChoiceOption_0" type="checkbox">

<input type="text" value="" id="ProjectManHoursUsed_becead30-410d-42de-872e-c12ad4c322b2_$NumberField" title="Man Hours Used" size="11" class="ms-input" style="ime-mode : inactive">

现在我想在 jQuery 中做什么,如果选中复选框,那么用户必须在输入字段中输入一个值,我尝试了这个但它没有用(警报永远不会显示!)

if ($("[id^=ProjectManHoursUsed_]").value === "" && $("[id^=ClientManagerApproval_]").is(':checked'))
    
    {
    
    alert("Please enter Man Hour Used before setting the stage to Closure approval");
    result = false;
    
    }

标签: javascriptjquery

解决方案


JQuery 有自己的获取值的函数。代替

$("[id^=ProjectManHoursUsed_]").value

经过

$("[id^=ProjectManHoursUsed_]").val()

看这里:

$("button").on("click", function(){

    if ($("[id^=ProjectManHoursUsed_]").val() === "" && $("[id^=ClientManagerApproval_]").is(':checked')){
        alert("Please enter Man Hour Used before setting the stage to Closure approval");
        result = false;
    }

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id="ClientManagerApproval_e565da24-d454-4537-b902-771a37689e9d_MultiChoiceOption_0" type="checkbox">
<input type="text" value="" id="ProjectManHoursUsed_becead30-410d-42de-872e-c12ad4c322b2_$NumberField" title="Man Hours Used" size="11" class="ms-input" style="ime-mode : inactive">

<button>submit</button>


推荐阅读