[英]Check select box with jQuery
I am trying to check a select box value. 我正在尝试检查选择框的值。 I know this question is asked earlier on Stack, but I can't get it work with these questions.
我知道这个问题是在Stack上提早问的,但是我无法解决这些问题。
This is my select form: 这是我选择的形式:
<ul id="config-steps">
<span class="step-number">4</span>
<select id="type">
<option value="">-- <?php _e( 'Select glasstype' ); ?> --</option>
<option value="1"><?php _e( 'Mat' ); ?></option>
<option value="2"><?php _e( 'Glossy' ); ?></option>
</select>
</ul><!--End config-steps-->
jQuery: jQuery的:
$( "#config-steps #type" ).change( function() {
var slct = $( this );
var type = slct.children( ":selected" ).val()
if( type === "" ) {
slct.parents('li').find('.step-number').removeClass( 'unvalid-step' ).addClass( 'valid-step' );
} else {
slct.parents('li').find('.step-number').removeClass( 'valid-step' ).addClass( 'unvalid-step' );
}
});
You need to use the val()
on the control and not its children. 您需要在控件上使用
val()
而不是其子级。
Replace the below code: 替换以下代码:
var type = slct.children( ":selected" ).val()
With this: 有了这个:
var type = slc.val();
So you will be having: 因此,您将拥有:
$( "#config-steps #type" ).change( function() {
var slct = $( this );
var type = slct.val();
if( type === "" ) {
input.parents('li').find('.step-number').removeClass( 'unvalid-step' ).addClass( 'valid-step' );
} else {
input.parents('li').find('.step-number').removeClass( 'valid-step' ).addClass( 'unvalid-step' );
}
});
you have to use val()
. 您必须使用
val()
。 it will give you the selected option value. 它将为您提供选定的选项值。
do like this: 这样做:
$( "#config-steps #type" ).change( function() {
var slct = $(this);
var type = slct.val();
if( type === "" ) {
input.parents('li').find('.step-number').removeClass( 'unvalid-step' ).addClass( 'valid-step' );
} else {
input.parents('li').find('.step-number').removeClass( 'valid-step' ).addClass( 'unvalid-step' );
}
});
and you can remove extra variable like this: 您可以这样删除多余的变量:
var type = $(this).val();
Revised: 修改后:
$( "#config-steps #type" ).change( function() {
if($(this).val() === "" ) {
input.parents('li').find('.step-number').removeClass( 'unvalid-step' ).addClass( 'valid-step' );
} else {
input.parents('li').find('.step-number').removeClass( 'valid-step' ).addClass( 'unvalid-step' );
}
});
You should simply call val() on your select element 您只需在选择元素上调用val()
Code: 码:
<select id="type">
<option value="0">A</option>
<option value="1">B</option>
<option value="2"><?php _e( 'Glossy' ); ?></option>
</select>
<input type="button" class="btn" value="Press"/>
$('.btn').click(function () {
alert($("#type").val());
});
Get the select box value by its id 通过其ID获取选择框值
ie 即
var type = $("#type").val(); var type = $(“#type”)。val();
$( "#config-steps #type" ).change( function() {
var type = $("#type").val(); //get Value by using its id
if( type === "" ) {
input.parents('li').find('.step-number').removeClass( 'unvalid-step' ).addClass( 'valid-step' );
} else {
input.parents('li').find('.step-number').removeClass( 'valid-step' ).addClass( 'unvalid-step' );
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.