我有这段代码可以根据选择生成某种形式。
$(document).ready(function(){
$('#dealerform').hide();
$('#customerform').hide();
$('#select').change(function(){
$('#dealerform,#customerform').hide();
$($(this).find('option:selected').attr('value')).show();
});
});
$(document).ready(function(){
$("input[name='emailquest']").change(function(){
if (this.value != "1") { // <----I would probably change this to look for this.checked
$("input[name='email']").prop("disabled", true);
} else {
$("input[name='email']").prop("disabled", false);
}
});
});
现在我已经添加了一些代码来尝试使用此代码向其中添加日期选择器,现在它破坏了我以前的代码,并且立即提供了整个内容,并且日期选择器不起作用。有人知道我在这里做错了什么并有可能的日期选择器解决方案吗?
$(function(){
$("#datepicker").datepicker();
});
谢谢
最佳答案
没有必要将每个代码块包装在新的 $(document.ready() 中。
这个:
$(document).ready(function(){
$('#dealerform').hide();
$('#customerform').hide();
$('#select').change(function(){
$('#dealerform,#customerform').hide();
$($(this).find('option:selected').attr('value')).show();
});
});
$(document).ready(function(){
$("input[name='emailquest']").change(function(){
if (this.value != "1") { // <----I would probably change this to look for this.checked
$("input[name='email']").prop("disabled", true);
} else {
$("input[name='email']").prop("disabled", false);
}
});
});
$(function(){
$("#datepicker").datepicker();
});
可以这样写:
$(function() {
// your first block of code
$('#dealerform').hide();
$('#customerform').hide();
$('#select').change(function(){
$('#dealerform,#customerform').hide();
$($(this).find('option:selected').attr('value')).show();
});
// your second block of code
$("input[name='emailquest']").change(function(){
if (this.value != "1") { // <----I would probably change this to look for this.checked
$("input[name='email']").prop("disabled", true);
}
else {
$("input[name='email']").prop("disabled", false);
}
});
// that last piece
$("#datepicker").datepicker();
})
<小时/>
请记住,如果您同时使用 jQuery 和 jQueryUI(datepicker 来自 jQueryUI),那么您必须引用这两个库,如下所示:
<html>
<head>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/smoothness/jquery-ui.min.css" type="text/css" media="all" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
// your first block of code
$('#dealerform').hide();
$('#customerform').hide();
$('#select').change(function(){
$('#dealerform,#customerform').hide();
$($(this).find('option:selected').attr('value')).show();
});
// your second block of code
$("input[name='emailquest']").change(function(){
if (this.value != "1") { // <----I would probably change this to look for this.checked
$("input[name='email']").prop("disabled", true);
}
else {
$("input[name='email']").prop("disabled", false);
}
});
// that last piece
$("#datepicker").datepicker();
})
</script>
</head>
关于javascript - JQuery 命令互相破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18407113/