不必将每个代码块包装在新的 $(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>