如何删除javascript中的选项
How to remove an option in javascript?
我不明白为什么这不起作用?不要删除小选项!
我有脚本:
<script>
var quantity_in_stock_s = 0
if ( quantity_in_stock_s === 0)
{
var S = document.getElementById("S");
document.getElementById("mySelect").removeChild(S);
}
</script>
和HTML:
<form>
<select id="mySelect">
<option id="S">Small</option>
<option id="M">Medium</option>
<option id="L">Large</option>
</select>
</form>
这错了吗?!!:
</head>
<body>
<form>
<select id="mySelect">
<option id="S">Small</option>
<option id="M">Medium</option>
<option id="L">Large</option>
</select>
</form>
<script>
var quantity_in_stock_S = 0
var quantity_in_stock_M = 0
var quantity_in_stock_L = 0
function deleteOption1(){
if ( quantity_in_stock_S === 0)
{
var S = document.getElementById("S");
document.getElementById("mySelect").removeChild(S);
}
}
function deleteOption2(){
if ( quantity_in_stock_M === 0)
{
var M = document.getElementById("M");
document.getElementById("mySelect").removeChild(M);
}
}
function deleteOption3(){
if ( quantity_in_stock_L === 0)
{
var L = document.getElementById("L");
document.getElementById("mySelect").removeChild(L);
}
}
window.onload = deleteOption1;
window.onload = deleteOption2;
window.onload = deleteOption3;
</script>
它应该是window.onload = deleteOption;
而不是window.load = deleteOption;
如果事实如此,这里有一个修改后的脚本:
<form>
<select id="mySelect">
<option id="S">Small</option>
<option id="M">Medium</option>
<option id="L">Large</option>
</select>
</form>
<script type="text/javascript">
var quantity_in_stock_S = 0
var quantity_in_stock_M = 0
var quantity_in_stock_L = 0
function deleteOptions() {
if ( quantity_in_stock_S === 0) {
var S = document.getElementById("S");
document.getElementById("mySelect").removeChild(S);
}
if ( quantity_in_stock_M === 0) {
var M = document.getElementById("M");
document.getElementById("mySelect").removeChild(M);
}
if ( quantity_in_stock_L === 0) {
var L = document.getElementById("L");
document.getElementById("mySelect").removeChild(L);
}
}
window.onload = function() {
deleteOptions();
};
</script>
或者。。。如果你想去Object Oriented
:
<form>
<select id="mySelect">
<option id="S">Small</option>
<option id="M">Medium</option>
<option id="L">Large</option>
</select>
</form>
<script type="text/javascript">
var Stocks = {
SmallStock: 0,
MediumStock: 0,
LargeStock: 0,
StockCheck: function() {
if (Stocks.SmallStock === 0) {
var S = document.getElementById("S");
document.getElementById("mySelect").removeChild(S);
}
if (Stocks.MediumStock === 0) {
var M = document.getElementById("M");
document.getElementById("mySelect").removeChild(M);
}
if (Stocks.LargeStock === 0) {
var L = document.getElementById("L");
document.getElementById("mySelect").removeChild(L);
}
}
};
window.onload = function() {
Stocks.StockCheck();
};
</script>
我认为您需要创建一个函数,然后根据您的需求在窗口加载或时调用该函数:
function deleteOption(){
var quantity_in_stock_s = 0
if ( quantity_in_stock_s === 0)
{
var S = document.getElementById("S");
document.getElementById("mySelect").removeChild(S);
}
}
window.load = deleteOption;
如果您的代码不工作,原因是您将它放在<head></head>
标记之间,而没有将它绑定到窗口/文档加载事件(window.onload
)。
尝试将相同的代码放在</body>
结束标记之前。别忘了把它放在<script></script>
标签之间。
相关文章:
- javascript选项卡没有'不起作用
- IE7在带有Javascript选项卡导航的页面上误读CSS
- 声明JavaScript选项对象的正确方法
- JavaScript选项卡动态宽度.animate()只能工作一次
- 编译为JavaScript选项,该选项使用十进制库而不是IEEE 754数学
- Javascript选项卡未正确激活
- Javascript选项卡字符删除
- Javascript 选项卡不会在页面加载时打开选项卡
- 显示隐藏样式 JavaScript 选项卡菜单
- 将鼠标悬停在 JavaScript 选项卡上,页面上有几个选项卡组
- JavaScript 选项卡式内容而不使用 jquery
- 如何直接链接到特定的 JavaScript 选项卡
- 在 JavaScript 选项卡中显示分页
- 预加载图像并为我的 CSS 行的简单 Javascript 选项过渡
- jQuery/JavaScript选项卡不适用于IE9
- 使用JavaScript启用和禁用浏览器JavaScript选项
- JavaScript选项卡式文件夹;无法在Internet Explorer中工作
- html/javascript选项卡控制器不工作
- 它能在JavaScript选项runat=“”中从另一台服务器调用js文件吗;服务器”;
- 雄辩的javascript选项卡练习