如何将选中的选项标记为选中? [英] How to mark the selected option as selected?
问题描述
我正在使用 ajax 分页,我让我的用户有机会选择每页记录,尽管提供了选项.问题是每次无论我选择哪个选项,第一个选项都保持选中状态!如何解决这个问题只有选定的选项仍然是选定的!
I am using ajax pagination where i am giving my users an opportunity to select record per page though provided option.The problem is every time the 1st option remains as selected no matter which option i am selecting!How to solve this problem so that only the selected option remains as selected!
jquery+ajax:
jquery+ajax:
<script type="text/javascript">
$(document).ready(function(){
changePagination('0');
});
function changePagination(pageId){
$(".flash").show();
$(".flash").fadeIn(800).html
('Loading <img src="ajax-loader.gif" />');
var dataString = {pageId: pageId, recordsPerPage: $('select[name="rp"] option:selected').html()};
type: "POST",
url: "page.php",
data: dataString,
cache: false,
success: function(result){
$(".flash").hide();
$("#pageData").html(result);
}
});
}
</script>
选项:
<select id="rp" name="rp" onChange=" changePagination();">
<option value="2">2</option>
<option value="6">6</option>
<option value="8">8</option>
<option value="10">10</option>
</select>
推荐答案
您可以选择其中一个选项并为其赋予 selected
属性.
You can select one of the options and give selected
property on it.
// 'n' will be your index value for selecting and making one of the options stay selected.
$('#rp option').eq(n).attr('selected', true);
$('#rp option').eq(n).attr('selected', 'selected');
<小时>
仅供参考
.attr()
在某个 jQuery 版本中无法正常工作.如果您遇到这种情况,请改用 prop()
.由于 selected
是一个属性,而不是一个属性,因此使用 prop()
在语义和综合上都是正确的.
.attr()
could not work properly in a certain jQuery version. If it happens to you, use prop()
instead. Since selected
is a property, not an attribute, using prop()
would be semantically and synthetically right.
$('#rp option').eq(n).prop('selected', true);
$('#rp option').eq(n).prop('selected', 'selected');
这篇关于如何将选中的选项标记为选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!