javascript - 如果选中复选框,用户必须输入一个值
问题描述
我有以下输入字段和一个复选框:-
<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;
}
解决方案
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>
推荐阅读
- php - PHP DateTime 输出范围内的日期数组
- python-3.x - 如何根据用户输入在 tkinter 中创建动态条目小部件并将这些条目小部件的输入存储在列表中
- angular - 我们如何订阅 Angular Material Tables 中当前显示的数据
- react-native - React-Native-Maps 停止无限滚动
- google-app-engine - PHP 7.2 的预热请求?
- selenium - 如何修复在 selenium-ide 中工作的 selenium-side-runner 中的选择
- .net-core - 使用不同的配置执行相同的测试
- c# - 为什么 SqlDependencyEx 会在一段时间后停止工作?
- angular - Webpack 魔术注释:动态生成 webpackInclude 表达式
- php - php:循环内的array_filter返回具有不同索引的数组