尝试在选择表单中的七个值之一时显示复选框
Trying to show a checkbox when one of seven values in a form is selected
如果选择了data-auth="need-purchase-auth"标记的七个职位之一,则尝试显示#authorized_to_purchase_box。
不知道为什么不工作。如果有更好的方法,请批评/告诉我。
HTML:<select name="job_title" id="job_title" onchange='showAuthBox(this);'>
<option selected disabled hidden style='display: none' value=''></option>
<option id="Superintendent" value="Superintendent">Superintendent</option>
<option id="Assistant-Superintendent" value="Assistant Superintendent">Assistant Superintendent</option>
<option id="Principal" value="Principal">Principal</option>
<option id="Vice-Principal" value="Vice Principal">Vice Principal</option>
<option id="Counselor" data-auth="need-purchase-auth" value="Counselor">Counselor</option>
<option id="Testing-Coordinator" data-auth="need-purchase-auth" value="Testing Coordinator">Testing Coordinator</option>
<option id="Curriculum-Director" data-auth="need-purchase-auth" value="Curriculum Director">Curriculum Director</option>
<option id="Administrator" data-auth="need-purchase-auth" value="Administrator">Administrator</option>
<option id="Teacher" data-auth="need-purchase-auth" value="Teacher">Teacher</option>
<option id="Parent" data-auth="need-purchase-auth" value="Parent">Parent</option>
<option id="Student" data-auth="need-purchase-auth" value="Student">Student</option>
</select>
Jquery: function showAuthBox(val) {
alert(val.dataset.auth);
if(val.dataset.auth == "need-purchase-auth") {
$('#authorized_to_purchase_box').css("display", "block");
} else {
$('#authorized_to_purchase_box').css("display", "none");
}
}
所以,我已经稍微修改了代码来使用jQuery的change事件。
代码如下:
$("#job_title").change(function() {
var auth = $(this).children(":selected").data('auth');
if (auth == 'need-purchase-auth') {
$("#authorized_to_purchase").show();
} else {
$("#authorized_to_purchase").hide();
}
});
基本上,我们在选择框上放置了一个事件侦听器,以便在选择不同选项时触发。然后,我们必须通过检查哪个选项具有:selected属性来查找哪个选项被选中了。然后,使用attr()方法获取data-auth属性值。最后,您只需要检查数据验证值是否等于您正在寻找的值。如果是,则显示复选框。如果不是,就把它藏起来。如果您需要对我的代码进行进一步的解释,请告诉我。
//bind the event handler in your script, not inline
$( '#job_title' ).on( 'change', function () {
if ( $( this ).find( 'option:checked' ).data( 'auth' ) === 'need-purchase-auth' ) {
//do stuff
console.log('weee');
}
} );
我将摆脱内联事件处理程序,并这样做:
$('#job_title').change(function showAuthBox(val) {
if ($('option:selected', this).data('auth') == "need-purchase-auth") {
$('#authorized_to_purchase_box').css("display", "block");
} else {
$('#authorized_to_purchase_box').css("display", "none");
}
})
#authorized_to_purchase_box {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="job_title" id="job_title">
<option selected disabled hidden style='display: none' value=''></option>
<option id="Superintendent" value="Superintendent">Superintendent</option>
<option id="Assistant-Superintendent" value="Assistant Superintendent">Assistant Superintendent</option>
<option id="Principal" value="Principal">Principal</option>
<option id="Vice-Principal" value="Vice Principal">Vice Principal</option>
<option id="Counselor" data-auth="need-purchase-auth" value="Counselor">Counselor</option>
<option id="Testing-Coordinator" data-auth="need-purchase-auth" value="Testing Coordinator">Testing Coordinator</option>
<option id="Curriculum-Director" data-auth="need-purchase-auth" value="Curriculum Director">Curriculum Director</option>
<option id="Administrator" data-auth="need-purchase-auth" value="Administrator">Administrator</option>
<option id="Teacher" data-auth="need-purchase-auth" value="Teacher">Teacher</option>
<option id="Parent" data-auth="need-purchase-auth" value="Parent">Parent</option>
<option id="Student" data-auth="need-purchase-auth" value="Student">Student</option>
</select>
<div id="authorized_to_purchase_box">
authorized_to_purchase_box
</div>
尝试收听#job_title的更改。然后检查所选选项是否为"need-purchase-auth"数据-auth。所以你最好把函数体改成这样:
$('select#job_title').on("change", function () {
var dataAuthVal = $(this).children('option:selected').attr('data-auth');
if ('need-purchase-auth' == dataAuthVal)
$('#authorized_to_purchase_box').css("display", "block");
else
$('#authorized_to_purchase_box').css("display", "none");
相关文章:
- 使用$.ajax发布多个复选框
- Angular JS Filter-通过3个复选框进行筛选
- 如果选中了多个复选框,如何添加事件
- 我有多个复选框,并希望为其中 2 个制定规则,以便如果我选择 1 个,则无法选择另一个
- 如何从javascript/jquery中的多个复选框中获取附加值
- 如何从多个复选框中获取最后一个值
- 在单击一个或多个复选框的同时调用iframe
- 如何在 Angular JS 中从关联数组创建多个复选框
- angular js多个复选框过滤多级JSON
- 如何获取多个复选框值并分配给隐藏字段
- 如何根据用户选择动态更改多个复选框的编号
- Jquery 多个复选框 if 语句
- 在数据库(JSP PAGE)中获取多个复选框结果
- 多个复选框,如果正确,则更改背景图像(HTML/Javascript)
- 检查多个复选框列表与单击一个按钮
- 传递多个复选框值
- 多个复选框过滤,JavaScript和jQuery
- Bootstrap下拉菜单中的复选框允许多个复选框而不是一个
- 如果在jQuery中选中,则返回多个复选框值
- 选中前n个复选框