我在html页面中有一个Select输入字段。在if块中,条件应该有效。
<select id='selectopt'>
<option value="">Select<option>
<option value="1">1<option>
</select>
<input type="text" id='text1'>
if($('#text1').val==='name'){
$("#selectopt option[value='']").attr('selected', true);
}
else{
}
当我把这个条件放在里面,如果它在chrome和IE中工作但不能在firefox中工作..
答案 0 :(得分:1)
使用$scope.addItem = function(){
$scope.stock.push({
name: $scope.newName,
price: parseInt($scope.newPrice),
color: $scope.newcolor,
avaliable: true
});
代替.prop()
获取匹配元素集中第一个元素的属性值,或为每个匹配元素设置一个或多个属性。
.attr()
答案 1 :(得分:0)
您正在将jquery对象与字符串进行比较
$('#text1') === 'name'
也许你想比较这个对象的价值?
$('#text1').val() === 'name'
您还可以通过以下方式简化设置下拉列表值:
$(&#34;#selectopt&#34)。VAL(&#34;&#34)
答案 2 :(得分:0)
$('#text1').val().trim()=== 'name'
</option>
标记未关闭prop()
代替attr()
if ($('#text1').val().trim()=== 'name') {
$("#selectopt option:empty").prop('selected', true);
} else {}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='selectopt'>
<option value="">Select<option>
<option value="1">1<option>
</select>
<input type="text" id='text1' value="name">
&#13;