1

我想显示 div “remove-untill-change”(及其内容),但前提是 Select 元素的选项已从其默认值更改。

必须在选项更改之前删除 div,而不是隐藏,如“display:none”和“visibility:hidden”。

我在下面制作了以下脚本,但这似乎不起作用:

演示:http: //jsfiddle.net/JZMYb/

<select id="the-changer">
  <option value="first">Do you like pie?</option>
  <option value="second">Yes</option>
  <option value="third">No</option>
</select>

<script language="javascript" type="text/javascript">
function hideShow(){
    document.write('<div id="remove-untill-change"><select><option value="first">Do you like chocolate mouse pie?</option><option value="second">Yes</option><option value="third">No</option></select></div>');
    if (document.getElementsByTagName('select').onchange = true);
};
</script>


任何人都知道适用于此示例的 Javascript(无 jQuery)吗?

4

1 回答 1

1

实际上您的脚本正在运行,您只是忘记调用您的函数

<select id="the-changer" onchange='removeShow()'><!--call the function-->
  <option value="first">Do you like pie?</option>
  <option value="second">Yes</option>
  <option value="third">No</option>
</select>

<script language="javascript" type="text/javascript">
function removeShow(){
    document.write('<div id="remove-untill-change"><select><option value="first">Do you like chocolate mouse pie?</option><option value="second">Yes</option><option value="third">No</option></select></div>');
    if (document.getElementsByTagName('select').onchange = true);
};
</script>

演示

更新基于Comments Demo 2 ​</p>

于 2012-11-19T11:19:49.493 回答