简体   繁体   English

我总是得到相同的HTML

[英]i always get the same html

i need some help with this, i'm trying to generate some html with javascript, but always get the same, this is my javascript code 我需要一些帮助,我正在尝试使用javascript生成一些html,但总是得到相同的结果,这是我的javascript代码

var datePickerHTML = function(id){
    var html = "<div id='"+id+"' class='ui-datepicker'>";
    html += "Hello World, my ID is "+id;
    html += "</div>";
    return html;
}

var UhmaCalendar = function(config){
    var dpID = 'dp'+config.inputField;
    this.dpID = dpID;
    jQuery('#'+config.inputField).after(function(){
      return datePickerHTML(dpID);
});
    jQuery('#'+config.btn).click(function(){
          jQuery('#'+dpID).toggle('slow');
    }
}

now, in my html i write 现在,在我的HTML中,我写

UhmaCalendar({
    btn : 'countday_pick',
    inputField : 'countday'
});
UhmaCalendar({
    btn : 'sartday_pick',
    inputField : 'startday'
});

this is for 2 diferent input and buttons, but they always show the same div with the same text, and id i check the html withthe firebug, only one div is created, ad i want 2 div with diferent ids i also try with 这是用于2个不同的输入和按钮,但它们始终显示具有相同文本的同一个div,并且id我用firebug检查html,仅创建了一个div,并且我想要2个具有不同id的div,我也尝试使用

new UhmaCalendar({
    btn : 'sartday_pick',
    inputField : 'startday'
});

but is the same, what am i doing wrong here thanks 但是还是一样,我在这里做错了什么,谢谢

You have forgotten to return html; 您忘记了return html; in the function datePickerHTML . 在函数datePickerHTML

With the newest edit, I was unable to reproduce the error. 使用最新的编辑,我无法重现该错误。 See http://jsbin.com/ucage4 for a demo which works correctly. 请参阅http://jsbin.com/ucage4 ,以获取可以正常运行的演示。

Hard to tell what's wrong, the code as quoted is unparseable. 很难说出问题所在,引用的代码不可解析。 Some notes that may be of use: 一些可能有用的注释:

var datePickerHTML = function(id){
    var html = "<div id='"+id+"' class='ui-datepicker'>";
    html += "Hello World, my ID is "+id;
    html += "</div>";
    // Should probably have `return html;` here
}

var UhmaCalendar = function(config){
    this.dpID = 'dp'+config.inputField;
    jQuery('#'+config.inputField).after(function(){
      return datePickerHTML(dpID);
                         // ^-- This symbol is undefined, and != `this.dbID`
    });
    jQuery('#'+config.btn).click(function(){
          jQuery('#'dpID).toggle('slow');
                 // ^-- syntax error (and `dbID` is undefined here too)
    } // <-- Missing `);` here
}

If I had to guess at the intent: 如果我必须猜测一下意图:

var datePickerHTML = function(id){
    var html = "<div id='"+id+"' class='ui-datepicker'>";
    html += "Hello World, my ID is "+id;
    html += "</div>";
    return html; // tjc
}

var UhmaCalendar = function(config){
    var dpID = 'dp'+config.inputField; // tjc
    this.dpID = dpID; // tjc
    jQuery('#'+config.inputField).after(function(){
      return datePickerHTML(dpID);
    });
    jQuery('#'+config.btn).click(function(){
          jQuery('#'+dpID).toggle('slow'); // tjc
    }); // tjc
}

My suspicion is that the dpID thing(s) are the main error. 我怀疑dpID是主要错误。 I've fixed it above by creating a variable within the function which the enclosed functions inherit access to (because they're closures). 我已经通过在函数中创建一个变量来解决此问题,该函数使封闭的函数继承访问权限(因为它们是闭包)。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM