取得当前选中option的属性还可以用这个:$("#selroom").find(" option:selected").attr("status");
$("#selroom").change(function () { alert($("#selroom option:selected").attr("status")); });$("#select的id").val()
$("#select的id>option:selected").val()
jQuery获取Select元素,并选择的Text和Value:
假如有这么一个select
1
2
3
4
|
<
select
id
=
"sel"
>
<
option
value
=
"1"
id
=
"id1"
>www.aerchi.com</
option
>
<
option
value
=
"2"
id
=
"id2"
>www.luxi.ren</
option
>
</
select
>
|
调用:
1
|
alert($(
":selected"
,
"#sel"
).attr(
"id"
));
|
即可
$("#thisselectID option:selected").attr("id");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
tr
>
<
td
class
=
"tdal"
> 所属部门:
</
td
>
<
td
class
=
"tdar"
>
<
select
id
=
"ipt_section"
class
=
"easyui-combobox"
name
=
"ipt_section"
editable
=
"false"
>
<
option
value
=
"0"
>采矿部</
option
>
<
option
value
=
"1"
>生产调度科</
option
>
<
option
value
=
"2"
>技术科</
option
>
<
option
value
=
"3"
>综合办</
option
>
<
option
value
=
"4"
>党群工作室</
option
>
<
option
value
=
"5"
>机电维修部</
option
>
<
option
value
=
"6"
>储运部</
option
>
</
select
>
</
td
>
</
tr
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
$(
'#tab_list'
).datagrid({
title:
'用户列表'
,
//表格标题
url: location.href,
//请求数据的页面
sortName:
'UserFile_id'
,
//排序字段
idField:
'UserFile_id'
,
//标识字段,主键
iconCls:
''
,
//标题左边的图标
width:
'100%'
,
//宽度
height: $(parent.document).find(
"#mainPanle"
).height() - 10 > 0 ? $(parent.document).find(
"#mainPanle"
).height() - 10 : 500,
//高度
nowrap:
false
,
//是否换行,True 就会把数据显示在一行里
striped:
true
,
//True 奇偶行使用不同背景色
collapsible:
false
,
//可折叠
sortOrder:
'desc'
,
//排序类型
remoteSort:
true
,
//定义是否从服务器给数据排序
frozenColumns: [[
//冻结的列,不会随横向滚动轴移动
{ field:
'cbx'
, checkbox:
true
},
{ title:
'用户编号'
, field:
'UserFile_num'
, width: 60, sortable:
true
},
{ title:
'用户账号名'
, field:
'UserFile_name'
, width: 100 }
]],
columns: [[
{ title:
'密码'
, field:
'UserFile_pwd'
, width: 120 },
{ title:
'用户性别'
, field:
'UserFile_sex'
, formatter:
function
(value, rec, index) {
return
value == 0 ?
'女'
:
'男'
}, width: 80 },
{ title:
'所属部门'
, field:
'UserFile_section'
,width: 100 },
{ title:
'职位'
, field:
'UserFile_post'
, width: 100 },
{ title:
'电话号码'
, field:
'UserFile_telephone'
, width: 100 },
{ title:
'邮箱'
, field:
'UserFile_email'
, width: 150 },
{
title:
'操作'
, field:
'UserFile_id'
, width: 80, formatter:
function
(value, rec) {
return
'<a style="color:red" href="javascript:;" onclick="EditData('
+ value +
');$(this).parent().click();return false;">修改</a>'
;
}
}
]],
toolbar:
"#tab_toolbar"
,
queryParams: {
"action"
:
"query"
},
pagination:
true
,
//是否开启分页
pageNumber: 1,
//默认索引页
pageSize: 10,
//默认一页数据条数
rownumbers:
true
//行号
});
|
(function ($) { $.widget("ui.combobox", { _create: function () { var self = this, select = this.element.hide(), selected = select.children(":selected"), value = selected.val() ? selected.text() : "", regSearch = /^[^a-zA-Z0-9]*([a-zA-Z0-9])/i, comboData = select.children("option").map(function () { if (this.value ) { var text = $(this).text(), labelHtml = self.options.label ? self.options.label(this) : text; //allows list customization return { label: labelHtml, value: text, option: this }; } }); var input = this.input = $("<input type='text' />") .insertAfter(select) .val(value) .keydown( function( event ) { var keyCode = $.ui.keyCode; switch( event.keyCode ) { case keyCode.PAGE_UP: case keyCode.PAGE_DOWN: case keyCode.UP: case keyCode.DOWN: case keyCode.ENTER: case keyCode.NUMPAD_ENTER: case keyCode.TAB: case keyCode.ESCAPE: //let autocomplete handle these break; default: //prevent autocomplete doing anything event.stopImmediatePropagation(); //only react to [a-zA-Z0-9] if ((event.keyCode < 91 && event.keyCode > 59) || (event.keyCode < 58 && event.keyCode > 47)) { var str = String.fromCharCode(event.keyCode).toLowerCase(), currVal = input.val(), opt; //find all options whose first alpha character matches that pressed var matchOpt = select.children().filter(function() { var test = regSearch.exec(this.text); return (test && test.length == 2 && test[1].toLowerCase() == str); }); if (!matchOpt.length ) return false; //if there is something selected we need to find the next in the list if (currVal.length) { var test = regSearch.exec(currVal); if (test && test.length == 2 && test[1].toLowerCase() == str) { //the next one that begins with that letter matchOpt.each(function(ix, el) { if (el.selected) { if ((ix + 1) <= matchOpt.length-1) { opt = matchOpt[ix + 1]; } return false; } }); } } //fallback to the first one that begins with that character if (!opt) opt = matchOpt[0]; //select that item opt.selected = true; input.val(opt.text); //if the dropdown is open, find it in the list if (input.autocomplete("widget").is(":visible")) { input.data("autocomplete").widget().children('li').each(function() { var $li = $(this); if ($li.data("item.autocomplete").option == opt) { input.data("autocomplete").menu.activate(event,$li); return false; } }); } } //ignore all other keystrokes return false; break; } }) .autocomplete({ delay: 0, minLength: 0, source: function (request, response) { response(comboData); }, select: function (event, ui) { ui.item.option.selected = true; self._trigger("selected", event, { item: ui.item.option }); }, change: function (event, ui) { if (!ui.item) { var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"), valid = false; select.children("option").each(function () { if ($(this).text().match(matcher)) { this.selected = valid = true; return false; } }); if (!valid) { // remove invalid value, as it didn't match anything $(this).val(""); select.val(""); input.data("autocomplete").term = ""; return false; } } } }) .addClass("ui-widget ui-widget-content ui-corner-left") .click(function() { self.button.click(); }) .bind("autocompleteopen", function(event, ui){ //find the currently selected item and highlight it in the list var opt = select.children(":selected")[0]; input.data("autocomplete").widget().children('li').each(function() { var $li = $(this); if ($li.data("item.autocomplete").option == opt) { input.data("autocomplete").menu.activate(event,$li); return false; } }); }); input.data("autocomplete")._renderItem = function (ul, item) { return $("<li></li>") .data("item.autocomplete", item) .append("<a href='#'>" + item.label + "</a>") .appendTo(ul); }; this.button = $("<button type='button'> </button>") .attr("tabIndex", -1) .attr("title", "Show All Items") .insertAfter(input) .button({ icons: { primary: "ui-icon-triangle-1-s" }, text: false }) .removeClass("ui-corner-all") .addClass("ui-corner-right ui-button-icon") .click(function () { // close if already visible if (input.autocomplete("widget").is(":visible")) { input.autocomplete("close"); return; } // pass empty string as value to search for, displaying all results input.autocomplete("search", ""); input.focus(); }); }, //allows programmatic selection of combo using the option value setValue: function (value) { var $input = this.input; $("option", this.element).each(function () { if ($(this).val() == value) { this.selected = true; $input.val(this.text); return false; } }); }, destroy: function () { this.input.remove(); this.button.remove(); this.element.show(); $.Widget.prototype.destroy.call(this); } }); })(jQuery);
乐意黎.
本文地址: http://blog.csdn.net/aerchi/article/details/51074810