这是我面临的问题的背景。
我有 Visualforce 页面(Salesforce),它有一个查找字段。
查找字段是一组隐藏字段、一个普通输入字段和带有 href 的图像,它会打开一个弹出窗口供用户选择。每个选定的值有 2 个值
- 所选值的名称
- 所选值的 id。
名称存储在非隐藏输入字段中,并存储在以 lkid 结尾的隐藏字段中。Html 看起来像这样
<div id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:optableAccount" style="position:relative">
//Below hidden field stores the id
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkid" type="hidden" value="001i0000008OJ9R" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkid">
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkold" type="hidden" value="Abbott Insurance" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkold">
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lktp" type="hidden" value="001" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lktp">
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lspf" type="hidden" value="0" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lspf">
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lspfsub" type="hidden" value="0" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lspfsub">
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_mod" type="hidden" value="1" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_mod">
<span class="lookupInput"> // Below input stores the name
<input id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup" class="accClass" type="text" size="20" onchange="getElementByIdCS('leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkid').value='';getElementByIdCS('leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_mod').value='1';" name="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup" maxlength="255" style="opacity: 0.1; width: 250px;">
<script>
<a id="leadConversionPage:leadConversionForm:pBConvertLead:pbSectionLeadSection:pbsiAccountName:accountLookup_lkwgt" class="accClass" title="Account Name Lookup (New Window)" onclick="setLastMousePosition(event)" href="javascript:%20openLookup%28%27%2F_ui%2Fcommon%2Fdata%2FLookupPage%3Flkfm%3DleadConversionPage%253AleadConversionForm%26lknm%3DleadConversionPage%253AleadConversionForm%253ApBConvertLead%253ApbSectionLeadSection%253ApbsiAccountName%253AaccountLookup%26lktp%3D%27%20%2B%20getElementByIdCS%28%27leadConversionPage%3AleadConversionForm%3ApBConvertLead%3ApbSectionLeadSection%3ApbsiAccountName%3AaccountLookup_lktp%27%29.value%2C670%2C%271%27%2C%27%26lksrch%3D%27%20%2B%20escapeUTF%28getElementByIdCS%28%27leadConversionPage%3AleadConversionForm%3ApBConvertLead%3ApbSectionLeadSection%3ApbsiAccountName%3AaccountLookup%27%29.value.substring%280%2C%2080%29%29%29">
</span>
<select id="someList" class="accSelectandlookup" >
</div>
每当名称更改时,id 也会更改。当名称更改时,我需要捕获 id 并将它们添加到选择中
查找的名称和 ID 是 accountLookup,为了获得名称,我可以搜索所有以 accountLookup 结尾的输入。我可以在我的表单中有多个查找,但我只需要此查找的名称和 ID,它的名称/ID 中包含 accountLookup 以附加到选择中。
这是我到目前为止在 SO 中一些伟人的帮助下完成的 jquery。我面临的问题是我没有得到 id 值。它的空白
$('input[id*="accountLookup"]').change(function() {
var accid ='';
//Loop through all the textboxes and find the one with lkid
$.each(['up_lkid'], function(i,element) {
accid = getInputValue(element); // Get the value of the inputbox ending with lkid
alert('llllll' + accid);
});
var accname= $(this).val();
//Get the closest div and find for an elemnet which has class of accSelectLookup
var selects = $(this).closest('div').find('.accSelectandlookup') ;
//Check if the accid is already present in the selectoptions and if not then add
if (!selects.find('option[value="' + accid + '"]').length) {
selects.append('<option value="' + accid + '">Attach to Existing : ' + accname + '</option>');
}
});
function getInputValue(endWith) {
return $('input[id$="' + endWith + '"]').val();
}
});