Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery,选项值_Jquery - Fatal编程技术网

jquery,选项值

jquery,选项值,jquery,Jquery,我的代码怎么了 为什么我没有得到选项值(在我的示例-1中) $(文档).ready(函数(){ 如果($(“#数字选项:选定”)。长度>0) { 警报('selected id:'+$(“#number option:selected”); } }); 一 二 三 只返回元素。您需要获取元素的值属性 您可以使用来获取值 alert( 'selected id:' + $("#number option:selected").val() ); 实际上,您可以对元素本身调用.val(),而

我的代码怎么了

为什么我没有得到选项值(在我的示例-1中)


$(文档).ready(函数(){
如果($(“#数字选项:选定”)。长度>0)
{   
警报('selected id:'+$(“#number option:selected”);
}
});
一
二
三

只返回元素。您需要获取元素的
属性

您可以使用来获取值

alert( 'selected id:' + $("#number option:selected").val() );
实际上,您可以对
元素本身调用
.val()
,而不是获取所选的


它只返回元素。您需要获取元素的
属性

您可以使用来获取值

alert( 'selected id:' + $("#number option:selected").val() );
实际上,您可以对
元素本身调用
.val()
,而不是获取所选的


它不起作用,因为您试图警告jQuery对象本身,而不是所选的值

而是像这样使用:

$(document).ready(function(){
  alert('selected id:' + $("#numberd").val());
});
$(document).ready(function(){
  alert('selected id:' + $("#numberd option:selected").text());
});
这将获得select的值。如果您想要文本(例如“一”),请使用以下命令:

$(document).ready(function(){
  alert('selected id:' + $("#numberd").val());
});
$(document).ready(function(){
  alert('selected id:' + $("#numberd option:selected").text());
});

第一个
将始终被选中,如果没有其他内容,则无需在此处检查匹配项的数量。

它不起作用,因为您试图提醒jQuery对象本身,而不是所选的值

而是像这样使用:

$(document).ready(function(){
  alert('selected id:' + $("#numberd").val());
});
$(document).ready(function(){
  alert('selected id:' + $("#numberd option:selected").text());
});
这将获得select的值。如果您想要文本(例如“一”),请使用以下命令:

$(document).ready(function(){
  alert('selected id:' + $("#numberd").val());
});
$(document).ready(function(){
  alert('selected id:' + $("#numberd option:selected").text());
});
如果没有其他选项,则将始终选择第一个
,因此无需在此处检查匹配项的数量