我如何检查是否选择了第一个选项,然后执行一些代码.
有点像,如果选择名为"My_Saved_Shipping"的框选择了第一个选项,则执行代码.
<select name="My_Saved_Shipping">
<option>Select/Enter Address</option>
<option value="1">text</option>
<option value="2">text2</option>
</select>
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 19
if ( $('select[name=My_Saved_Shipping]')[0].selectedIndex === 0 ) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
最好给出<select>一个id(通常匹配name) - #id选择器比[attr=val]选择器快.
Mes*_*sci 17
这是另一种方法:
$('.myselect').change(function() {
if ($(this).children('option:first-child').is(':selected')) {
//do something
}
});
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript">
if($("select[name='My_Saved_Shipping']").selectedIndex == 0)
{
//This is where your code goes
alert('First Element is selected');
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果要测试其中的文本option,请使用:
<script type="text/javascript">
if($("select[name='My_Saved_Shipping']").text == 'Select/Enter Address')
{
//This is where your code goes
alert('First Element is selected');
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18752 次 |
| 最近记录: |