Javascript 使用jquery在组合框中添加值
我是jquery的新手。 我有一个jsp表单,我在onload中调用javascript函数。 我想使用jquery-1.4.2.js在iframe中的组合框中加载一些值,但无法添加 请任何人给出解决方案 提前谢谢 这是我的jsp代码 这是我的jquery代码Javascript 使用jquery在组合框中添加值,javascript,jquery,jsp,Javascript,Jquery,Jsp,我是jquery的新手。 我有一个jsp表单,我在onload中调用javascript函数。 我想使用jquery-1.4.2.js在iframe中的组合框中加载一些值,但无法添加 请任何人给出解决方案 提前谢谢 这是我的jsp代码 这是我的jquery代码 xmlDoc是我的jsone回复,其中包含国家/地区列表 var frm = $("#applyMultipleInnerFrame").contents()); for(var i=0;i<5;i++) { if (
xmlDoc
是我的jsone回复,其中包含国家/地区列表
var frm = $("#applyMultipleInnerFrame").contents());
for(var i=0;i<5;i++) {
if ($(frm).find('input[name=candidateCountry_' + i + ']') && $(frm).find('input[name=candidateCountry_' + i + '] option')) {
$(frm).find('input[name=candidateCountry_' + i + '] option').length = 0;
if (xmlDoc.countryList) {
$(frm).find('input[name=candidateCountry_' + i + '] option')[$(frm).find('input[name=candidateCountry_' + i + '] option').length] = new Option("-- Select --", 0);
for (var k = 0; k < xmlDoc.countryList.length; k++) {
$(frm).find('input[name=candidateCountry_' + i + '] option')[$(frm).find('input[name=candidateCountry_' + i + '] option').length] = new Option(xmlDoc.countryList[k].name, xmlDoc.countryList[k].id);
if ((xmlDoc.countryList[k].id) == dCountryId) {
alert($($(frm).find('input[name=candidateCountry_' + i + ']').text()));
//alert($(frm).find('input[name=candidateCountry_'+i+']').val(dCountryId).text());
//$(frm).find('input[name=candidateCountry_"+i+"] option').eq(dCountryId).attr('selected', 'selected');
}
}
}
//alert($(frm).find('input[name=candidateCountry_'+i+']) option:selected').text());
//processAjaxRequestPost('ajaxRequestPost','SingleListHandler','getStateListDetails', eval('frm.candidateCountry_'+i).options[eval('frm.candidateCountry_'+i).selectedIndex].value);
}
}
var frm=$(“#applyMultipleInnerFrame”).contents();
就我所知,对于(var i=0;i,您无法(轻松地)修改iframe的内容,因为它们有自己的DOM。如果iframe中的内容来自您(例如,page1.html包含包含page2.html的iframe),而不是外部源,您可以将脚本插入page2.html,这样就不必进行跨DOM操作(我去过那里——太糟糕了)
另外,你的代码看起来像是吞没了核武器。如果你清理了代码并只发布了相关部分,我相信你会得到更多帮助;)要访问iframe的内容,你必须使用
.contents()方法还可用于获取文档的内容
如果iframe与主页位于同一域中,则为iframe
现在,关于您的代码,它真的很混乱。
在编写jquery代码时,请记住以下几点:
- 查询效率不高,请尽可能频繁地缓存查询(例如,对于固定元素)
- 要创建标记,最有效的方法是将html构建为文本,并立即附加整个块
jQuery(“”)(或$(“”))
始终返回一个对象。要检查查询返回的结果,请检查length
属性
下面是一段经过优化的代码:
// cache the elements you're going to use
var $frm = $(frm),
$candidCountry = $(frm).find('input[name=candidateCountry_' + i + ']');
// always check the length property to verify your query returned results
// because $('') always returns
if ($candidCountry.length && $candidCountry.find('option').length) {
var html;
if (xmlDoc.countryList) {
// build the html as text
html += '<option value="0">-- Select --</option>';
for (var k = 0; k < xmlDoc.countryList.length; k++) {
html += '<option value="'+xmlDoc.countryList[k].id+'">'+xmlDoc.countryList[k].name+'</option>';
}
}
// append the whole html at once
$candidCountry.html(html);
}
//缓存要使用的元素
风险值$frm=$(frm),
$candidCountry=$(frm).find('input[name=candidateCountry_'+i+']);
//始终检查length属性以验证查询返回的结果
//因为$('')总是返回
if($candidCountry.length&&$candidCountry.find('option').length){
var-html;
if(xmlDoc.countryList){
//将html构建为文本
html+='--选择--';
for(var k=0;k
您应该真正缓存$(frm)
而不是反复调用它:var$frm=$(frm)
然后使用$frm
谢谢您的回复…我将代码样式更改为您的代码样式,但没有得到结果。请告诉我如何使用jquery获取iframe对象我认为您应该使用var$candidCountry=$(“#applyMultipleInnerFrame”).contents().find('input[name=candidateCountry_'+i+'))
frame id=applyMultipleInnerFrame和iframe jsp form id=applymultiplecandidatesumedata我尝试获取表单对象var frm=$(“#applyMultipleInnerFrame”).contents().find(“#applymultiplecandidatesumedata”);为什么不执行ajax请求来获取数据,而不是使用iframe?使用iframe很棘手。。。
var frm = $("#applyMultipleInnerFrame").contents());
var frm = $("#applyMultipleInnerFrame").contents());
for(var i=0;i<5;i++) {
if ($(frm).find('input[name=candidateCountry_' + i + ']') && $(frm).find('input[name=candidateCountry_' + i + '] option')) {
$(frm).find('input[name=candidateCountry_' + i + '] option').length = 0;
if (xmlDoc.countryList) {
$(frm).find('input[name=candidateCountry_' + i + '] option')[$(frm).find('input[name=candidateCountry_' + i + '] option').length] = new Option("-- Select --", 0);
for (var k = 0; k < xmlDoc.countryList.length; k++) {
$(frm).find('input[name=candidateCountry_' + i + '] option')[$(frm).find('input[name=candidateCountry_' + i + '] option').length] = new Option(xmlDoc.countryList[k].name, xmlDoc.countryList[k].id);
if ((xmlDoc.countryList[k].id) == dCountryId) {
alert($($(frm).find('input[name=candidateCountry_' + i + ']').text()));
//alert($(frm).find('input[name=candidateCountry_'+i+']').val(dCountryId).text());
//$(frm).find('input[name=candidateCountry_"+i+"] option').eq(dCountryId).attr('selected', 'selected');
}
}
}
//alert($(frm).find('input[name=candidateCountry_'+i+']) option:selected').text());
//processAjaxRequestPost('ajaxRequestPost','SingleListHandler','getStateListDetails', eval('frm.candidateCountry_'+i).options[eval('frm.candidateCountry_'+i).selectedIndex].value);
}
}
var $frm = $(frm); // then use $frm
// cache the elements you're going to use
var $frm = $(frm),
$candidCountry = $(frm).find('input[name=candidateCountry_' + i + ']');
// always check the length property to verify your query returned results
// because $('') always returns
if ($candidCountry.length && $candidCountry.find('option').length) {
var html;
if (xmlDoc.countryList) {
// build the html as text
html += '<option value="0">-- Select --</option>';
for (var k = 0; k < xmlDoc.countryList.length; k++) {
html += '<option value="'+xmlDoc.countryList[k].id+'">'+xmlDoc.countryList[k].name+'</option>';
}
}
// append the whole html at once
$candidCountry.html(html);
}