根据所选选项更改 div 的 JavaScript 函数 - 并不总是按预期工作?
我有一个 JavaScript 函数(图片),当访问者从下拉菜单 (picture_type) 中选择一个选项 (onchange) 时,该函数就会被激活,然后更改 div (picture_div) 内的数据。
然而,div 中的数据并不总是反映所选选项,例如:如果您选择“选择”选项...然后选择“URL”选项,则 div 不会反映“URL”的内容。
这是 JavaScript 函数以及相应的 HTML:
<script>
function picture() {
var picture_type = document.getElementById('picture_type').value;
if (picture_type == 2) {
document.getElementById('picture_div').innerHTML = '<a href="predefined_pics.php">Click here</a> to select one.';
}
if (picture_type >= 1) {
document.getElementById('picture_div').style.display = 'block';
} else {
document.getElementById('picture_div').style.display = 'none';
}
}
</script>
<select id="picture_type" onchange="return picture();">
<option value="0" selected="selected">Upload</option>
<option value="1">URL</option>
<option value="2">Select</option>
</select>
<div id="picture_div" style="display: none;">
URL: <input name="url" type="text" style="width: 100%" />
</div>
如果您不明白我的意思,我建议您测试上面的代码并使用选项来更好地理解我。 :)
非常感谢所有帮助!
I have a JavaScript function (picture), which is activated when the visitor selects an option (onchange) from the drop down menu (picture_type), which then changes the data within the div (picture_div).
However not always does the data within the div reflect the chosen option, e.g: If you select the 'Select' option...then select the 'URL' option, the div doesn't reflect what should be their for the 'URL'.
Heres the JavaScript function along with the appropriate HTML:
<script>
function picture() {
var picture_type = document.getElementById('picture_type').value;
if (picture_type == 2) {
document.getElementById('picture_div').innerHTML = '<a href="predefined_pics.php">Click here</a> to select one.';
}
if (picture_type >= 1) {
document.getElementById('picture_div').style.display = 'block';
} else {
document.getElementById('picture_div').style.display = 'none';
}
}
</script>
<select id="picture_type" onchange="return picture();">
<option value="0" selected="selected">Upload</option>
<option value="1">URL</option>
<option value="2">Select</option>
</select>
<div id="picture_div" style="display: none;">
URL: <input name="url" type="text" style="width: 100%" />
</div>
If you don't understand me, I suggest you test the above code and play with the options to understand me better. :)
All help is greatly appreciated!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是代码,试试这个
,
Wazzy
Here is the code ,Try this out
with regards,
Wazzy
我认为代码中存在一些不一致之处。
仅当选择
URL
选项时,您才会更改picture_div
的内容,但当URL
或Select 时,会显示 div
被选中。我认为如果选择了
Select
选项,您需要更改picture_div
的值。其实这里的要求是什么呢?选择
URL
或Select
选项时是否必须显示picture_div
?如果是这种情况,那么您必须检查条件,因为此条件仅处理
Select
情况。您还必须处理值为1 (URL)
的情况。试试这个
I think there are some inconsistencies in the code.
You are changing the contents of the
picture_div
only when the optionURL
is selected, but the div is displayed when eitherURL
orSelect
is selected.I think you need to change the value of the
picture_div
if theSelect
option is selected.Actually what is the requirement here? Whether the
picture_div
has to be displayed whileURL
orSelect
options are selected? If that is the case then you have to check the conditionbecause this condition handles only the
Select
case. You've to handle the case when the value is1 (URL)
also.Try this