Javascript Duplicates Function 不起作用
Javascript Duplicates Function doesn't work
我有以下代码,我在小提琴中找到了它作为示例 http://jsfiddle.net/x4E5Q/1/。我希望有两个相同的选择输入,当我从第一个中选择值时,第二个上的相同值是不可选择的。我把所有的东西都放在一个文件中,但它不能按预期工作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function preventDupes( select, index ) {
var options = select.options,
len = options.length;
while( len-- ) {
options[ len ].disabled = false;
}
select.options[ index ].disabled = true;
if( index === select.selectedIndex ) {
alert('You''ve already selected the item "' + select.options[index].text + '".'n'nPlease choose another.');
this.selectedIndex = 0;
}
}
var select1 = select = document.getElementById( 'select1' );
var select2 = select = document.getElementById( 'select2' );
select1.onchange = function() {
preventDupes.call(this, select2, this.selectedIndex );
};
select2.onchange = function() {
preventDupes.call(this, select1, this.selectedIndex );
};
</script>
<select id="select1" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option> Accounting</option>
<option> Afrikaans</option>
<option> Applied Information and Communication Technology</option>
<option> Arabic</option>
<option> Art and Design</option>
<option> Biology</option>
<option> Business Studies</option>
</select>
<select id="select2" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option> Accounting</option>
<option> Afrikaans</option>
<option> Applied Information and Communication Technology</option>
<option> Arabic</option>
<option> Art and Design</option>
<option> Biology</option>
<option> Business Studies</option>
</select>
</body>
</html>
您需要在页面加载后触发代码,例如波纹管
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
window.addEventListener("load", function(){
function preventDupes( select, index ) {
var options = select.options,
len = options.length;
while( len-- ) {
options[ len ].disabled = false;
}
select.options[ index ].disabled = true;
if( index === select.selectedIndex ) {
alert('You''ve already selected the item "' + select.options[index].text + '".'n'nPlease choose another.');
this.selectedIndex = 0;
}
}
var select1 = select = document.getElementById( 'select1' );
var select2 = select = document.getElementById( 'select2' );
select1.onchange = function() {
preventDupes.call(this, select2, this.selectedIndex );
};
select2.onchange = function() {
preventDupes.call(this, select1, this.selectedIndex );
};
});
</script>
<select id="select1" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option> Accounting</option>
<option> Afrikaans</option>
<option> Applied Information and Communication Technology</option>
<option> Arabic</option>
<option> Art and Design</option>
<option> Biology</option>
<option> Business Studies</option>
</select>
<select id="select2" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option> Accounting</option>
<option> Afrikaans</option>
<option> Applied Information and Communication Technology</option>
<option> Arabic</option>
<option> Art and Design</option>
<option> Biology</option>
<option> Business Studies</option>
</select>
</body>
</html>
或者把脚本放在你用 DOM 编码之后...
<body>
<!-- your html -->
<script>
//your code
</script>
</body>
两种工作方式
你应该在window.load上运行js(或文档就绪)。只需将整个 js 包装在这样的函数中:
window.onload = function(){
//your code
}
小提琴之所以有效,是因为它默认设置为运行 js onLoad。如果您将其设置为头部 - 没有换行,它将不再起作用。
相关文章:
- Javascript Duplicates Function 不起作用
- function(){如果淡出,则追加}不起作用
- 如果我使用 JQuery $(window).click(function() 关闭菜单,菜单的墨水不起作用
- JQuery .on(“click”, function() ) 不起作用
- JQuery .click(function() 在 IE 中不起作用
- $(document).ready(function () 在弹出窗口打开后不起作用
- $(.class).click(function(){}) 不起作用
- JQuery ajax post function不起作用
- Jquery (ajax) load function 不起作用
- $('body').on('click', '.anything', function(){}) 在 iOS 上不起作用
- window.addEventListener(“加载”,function(){});不起作用
- 为什么function(){}不起作用,而(function()})起作用?(Chrome DevTools/Node)
- onchange to create autosave function(javascript)did'不起作用
- Var foo = (function(){..})();.. Foo()不起作用
- 未捕获的引用错误:$ is not defined(匿名函数)$(function()不起作用
- 对象.assign似乎对Function.prototype不起作用
- firefox中的href=javascript:function()不起作用
- 使用Object.create()和function.apply'似乎不起作用
- function(){}(args) 在 FF 中不起作用
- $(window).scroll(function)不起作用