我尝试将one2many关系字段的视图更改为optgroup,所以为此,我尝试在jquery中,所以首先将窗口小部件更改为选择,之后我尝试使用jquery。这里是我的jquery代码
<script type="text/javascript">
(function () {
var displayEduacationfield = function(){
var course_value = jQuery('#oe-field-input-13').val();
console.log("Hii");
console.log(course_value);
var course_value = jQuery('#oe-field-input-13').val();
if (course_value == "15"){
jQuery('select[name="x_pg_institution"]').hide();
}
};
displayEduacationfield();
var restructureCourseSelector = function () {
jQuery('select[name="x_course_id"]').each(function () {
var _self = jQuery(this);
var _parentSelect = _self.parent();
if(!_parentSelect.is('select'))
return;
var selfText = jQuery.trim(_self.text());
if(selfText == "")
return;
_self.data('originalText', selfText);
var selfText_a = selfText.split('-');
var groupName = selfText_a.shift();
var optionName = selfText_a.join('-');
_self.text(optionName);
var _group = _parentSelect.find('optgroup[label="'+groupName+'"]');
if(!_group.length) {
_group = jQuery(document.createElement("optgroup"));
console.log("hello");
_group.attr("label", groupName);
_parentSelect.append(_group);
}
_group.append(_self);
});
};
restructureCourseSelector();
})();
</script>
但我没有得到任何适当的解决方案。如果您有任何想法请与我分享。
答案 0 :(得分:0)
var restructureCourseSelector = function () {
jQuery('select[name="x_course_id"]').find('option').each(function ()
{
var _self = jQuery(this);
var _parentSelect = _self.parent();
if(!_parentSelect.is('select'))
return;
var selfText = jQuery.trim(_self.text());
if(selfText == "")
return;
_self.data('originalText', selfText);
var selfText_a = selfText.split('-');
var groupName = selfText_a.shift();
var optionName = selfText_a.join('-');
_self.text(optionName);
var _group = _parentSelect.find('optgroup[label="'+groupName+'"]');
if(!_group.length) {
_group = jQuery(document.createElement("optgroup"));
_group.attr("label", groupName);
_parentSelect.append(_group);
}
_group.append(_self);
});
};
jQuery( document ).ready(function() {
restructureCourseSelector();
});
jQuery( document ).ajaxComplete(function() {
restructureCourseSelector();
});
这一个像魅力一样......