我已经从另一个SO线程中跟随了这个例子: X-editable custom field type not respecting overridden defaults 它帮助我用x-editable创建了一个类似的自定义字段。一切正常,但我无法弄清楚如何渲染/显示选择的'文本' - SO示例输出'值'。我的js到initialis x-editable使用我的自定义输入因此:
$('#stffulladdress').editable({
url : '',
pk : 6,
value : {
address : "",
city : "",
region : "",
postcode : "",
country : "eng"
},
sourceCountry: [
{ value: "eng", text: "ENGLAND" },
{ value: "ire", text: "IRELAND" },
{ value: "sco", text: "SCOTLAND" },
{ value: "cym", text: "WALES" }
],
validate : function(value) {
if (value.address == '' || value.postcode == '') return "Address first lines AND Postcode required";
},
display : function(value) {
if (!value) {
$(this).empty();
return;
}
console.log(value.country);
var html = $('<div>').html( value.address + '<br />' + value.city + '<br />' + value.region + '<br />' + value.postcode + '<br />' + value.country );
$(this).html(html);
}
});
我的自定义x可编辑类与提到的SO示例相同,唯一的区别是我有更多输入 - 它们按预期工作正常。我原以为x-edit类中的这个片段会获取所选项目的文本:
value2html: function(value, element) {
if(!value) {
$(element).empty();
return;
}
var countryText = value.country;
$.each(this.sourceCountryData, function (i, v) {
if (v.value == countryText) {
countryText = v.text.toUpperCase();
}
});
var html = $('<div>').html( value.address + ',<br />' + value.city + ',<br />' + value.region + ',<br />' + value.postcode + ',<br />' + countryText );
$(element).html(html);
},
但是显示所选国家/地区我仍然获得国家/地区值“eng”而不是文本“England”。
我试过了:
value.country ,它给我的值'eng' 和 value.text 给出未定义的
知道如何获取所选文本吗?
感谢
答案 0 :(得分:0)
尝试使用sourceCountry,如下所示:
sourceCountry: [
{ "eng": "ENGLAND" },
{ "ire": "IRELAND" },
{ "sco": "SCOTLAND" },
{ "cym": "WALES" }
],