给你……使用 jQuery 1.8.3……
实验一:现场演示
代码
<select name="query" id="query">
<option value="join-us">Join Us</option>
<option value="work-with-us">Work with us</option>
<option value="about-pf">About Planet Fitness</option>
<option value="other">Other</option>
</select>
<script>
var query1 = $('#query option:selected').text();
var query2 = $('#query').val();
var query3 = $('#query').text();
alert('query1 = ' + query1 + '\nquery2 = ' + query2 + '\nquery3 = ' + query3);
console.log('query1 = ' + query1 + '\nquery2 = ' + query2 + '\nquery3 = ' + query3);
</script>
输出
铬合金:
query1 = Join Us
query2 = join-us
query3 =
Join Us
Work with us
About Planet Fitness
Other
IE:
query1 = Join Us
query2 = join-us
query3 = Join Us Work with us About Planet Fitness Other
火狐:
query1 = Join Us
query2 = join-us
query3 =
Join Us
Work with us
About Planet Fitness
Other
实验二:现场演示
代码
<script>
function getMyValue() {
var query1 = $('#query option:selected').text();
var query2 = $('#query').val();
var query3 = $('#query').text();
alert('query1 = ' + query1 + '\nquery2 = ' + query2 + '\nquery3 = ' + query3);
console.log('query1 = ' + query1 + '\nquery2 = ' + query2 + '\nquery3 = ' + query3);
}
</script>
</head>
<body>
<select name="query" id="query">
<option value="join-us">Join Us</option>
<option value="work-with-us">Work with us</option>
<option value="about-pf">About Planet Fitness</option>
<option value="other">Other</option>
</select>
<button onclick="getMyValue();">Click Me</button>
</body>
输出(选择“与我们合作”后单击按钮
铬合金:
query1 = Work with us
query2 = work-with-us
query3 =
Join Us
Work with us
About Planet Fitness
Other
IE:
query1 = Work with us
query2 = work-with-us
query3 = Join Us Work with us About Planet Fitness Other
火狐:
query1 = Work with us
query2 = work-with-us
query3 =
Join Us
Work with us
About Planet Fitness
Other
推理
Your code works fine, should be something wrong with the way you are invoking it, try putting alerts all over the place...