Javascript 根据下拉列表中的选择更新 div?
我的一个在线系统出现了一个小问题。 我们有一个部分提供 URL,供用户提供给他们的客户。这些 URL 位于几个 DIV 内,并且有一个包含语言选择的下拉列表,可根据所选语言更新 DIV 内的 URL。问题是,目前这仅在 IE 中有效。可能是一些糟糕的 JavaScript 代码,但我无法弄清楚。
这是 Javascript 函数:
<script language="JavaScript">
function changedLanguage(langCode, divNames)
{
var i;
for (i in divNames)
document.getElementById(divNames[i]).innerText = document.getElementById(divNames[i]).value.replace('Language_Code=','Language_Code='+langCod e);
}
</script>
function changedTemplate(templateid, divNames)
{
var i;
for (i in divNames)
{
if (templateid == '') {
document.getElementById(divNames[i]).innerText = document.getElementById('f' + divNames[i]).value;
} else {
document.getElementById(divNames[i]).innerText = document.getElementById('f' + divNames[i]).value.replace('template_id=','Role='+templateid+'&Registration=Y').replace('Registration.asp','PersonImport.asp');
};
};
}
这是下拉列表中的代码:
<SELECT NAME="lstLanguage" id="lstLanguage">
<OPTION VALUE="">-- Generic default ---</OPTION>
<OPTION ID="Arabic" VALUE="AR">Arabic</OPTION>
<OPTION ID="German" VALUE="D">German</OPTION>
</SELECT>
这是第二个下拉列表
<select id="lstTemplate" name="lstTemplate">
<option value="">-- Generic default ---</option>
<option value="4">Member</option>
<option value="5">Student</option>
</select>
DIV 容器的 ID 为:
'Ind_URL','Ind_W_URL','Login_URL','Login_W_URL','Group_URL','Group_W_URL','Group_Login_URL','Group_Login_W_URL','Holding_Login_URL','Holding_Login_W_URL','Pre_URL','Pre_W_URL'
第一滴down 需要更改所有参数DIV,第二个下拉列表只需更改前两个 div 上的参数('Ind_URL','Ind_W_URL')。
我尝试使用下面提供的 jQuery 代码,但在一个下拉菜单中效果很好,一旦我更改第二个下拉菜单中的选择,它就会清除一些 div 并且不会更改参数。
有什么想法吗?
I'm having a slight issue on one of our online system.
We have a section where we give URLs for the user to give to their clients. The urls are inside a couple of DIVs, and there is a dropdown with language selections that updates the URLs inside the DIVs according to the language selected. The problem is, that this is working only in IE at the moment. Probably some bad coded Javascript but I can't figure it out.
This is the Javascript function:
<script language="JavaScript">
function changedLanguage(langCode, divNames)
{
var i;
for (i in divNames)
document.getElementById(divNames[i]).innerText = document.getElementById(divNames[i]).value.replace('Language_Code=','Language_Code='+langCod e);
}
</script>
function changedTemplate(templateid, divNames)
{
var i;
for (i in divNames)
{
if (templateid == '') {
document.getElementById(divNames[i]).innerText = document.getElementById('f' + divNames[i]).value;
} else {
document.getElementById(divNames[i]).innerText = document.getElementById('f' + divNames[i]).value.replace('template_id=','Role='+templateid+'&Registration=Y').replace('Registration.asp','PersonImport.asp');
};
};
}
This is the code on the dropdown:
<SELECT NAME="lstLanguage" id="lstLanguage">
<OPTION VALUE="">-- Generic default ---</OPTION>
<OPTION ID="Arabic" VALUE="AR">Arabic</OPTION>
<OPTION ID="German" VALUE="D">German</OPTION>
</SELECT>
This is the second dropdown
<select id="lstTemplate" name="lstTemplate">
<option value="">-- Generic default ---</option>
<option value="4">Member</option>
<option value="5">Student</option>
</select>
The IDs of the DIV containers are:
'Ind_URL','Ind_W_URL','Login_URL','Login_W_URL','Group_URL','Group_W_URL','Group_Login_URL','Group_Login_W_URL','Holding_Login_URL','Holding_Login_W_URL','Pre_URL','Pre_W_URL'
The first drop down needs to change the parameter on all DIVs, the second drop down needs to change the parameter on only the first two divs ('Ind_URL','Ind_W_URL').
I tried using the jQuery code provided below but works perfect with one drop down, as soon as I change the selection on the second drop down it clears some of the divs and doesn't change the parameter.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
innerText 和outerText 仅适用于IE。请使用innerHTML而不是innerText。
更多信息
innerText and outerText only work in IE. Please use innerHTML instead of innerText.
More info
尝试使用 jQuery,它可以解决很多跨浏览器问题。像这样的东西:
像这样包含JavaScript:
可以在这里使用它: http://jsfiddle.net/sDFHg/< /a>
这是您正在寻找的东西吗?
Try using jQuery, it takes a lot of the cross-browser issues out of the equation. Something like this:
With JavaScript included like this:
Can play around with it here: http://jsfiddle.net/sDFHg/
Is that the sort of thing you were looking for?