我的jQuery代码有问题,如下所示。我在这里描述了我们遇到的问题。
var subnetCalculator = {
_data: {
_ipClass: $('#class_select'),
_givenOctet1: $('#given_octet1_text'),
_givenOctet2: $('#given_octet2_text'),
_givenOctet3: $('#given_octet3_text'),
_givenOctet4: $('#given_octet4_text'),
_cidr: $('#given_mask_text')
},
initProblem: function(){
var ipclass = this._data._ipClass.val();
alert(ipclass); //ipclass variable is showing undefined.
}
}
$("#problem_button").ready(function() { //This is not working on click function
console.log(5 + 6);
subnetCalculator.initProblem();
});
答案 0 :(得分:1)
似乎有些问题是这些:
ready
事件不是click
事件。在我看来,解决方案是将代码包装在doc ready块中,而不是ready
使用click
事件:
jQuery(document).ready(function($) {
var subnetCalculator = {
_data: {
_ipClass: $('#class_select'),
_givenOctet1: $('#given_octet1_text'),
_givenOctet2: $('#given_octet2_text'),
_givenOctet3: $('#given_octet3_text'),
_givenOctet4: $('#given_octet4_text'),
_cidr: $('#given_mask_text')
},
initProblem: function() {
var ipclass = this._data._ipClass.val();
alert(ipclass); //ipclass variable is showing undefined.
}
}
$("#problem_button").click(function() { // <------This should be "click"
console.log(5 + 6);
subnetCalculator.initProblem();
});
});
答案 1 :(得分:0)
尝试以下方法。您应该将函数绑定到click
事件。
var subnetCalculator = {
_data: {
_ipClass: $('#class_select'),
_givenOctet1: $('#given_octet1_text'),
_givenOctet2: $('#given_octet2_text'),
_givenOctet3: $('#given_octet3_text'),
_givenOctet4: $('#given_octet4_text'),
_cidr: $('#given_mask_text')
},
initProblem: function() {
var ipclass = this._data._ipClass.val();
alert(ipclass); * * //ipclass variable is showing undefined.**
}
}
$(function() {
$("#problem_button").click(function() {
console.log(5 + 6);
subnetCalculator.initProblem();
});
});