我試圖使用jQuery的日期選擇器函數來填充日誌字段是哈希映射中設置的對象的一部分。jQuery的Datepicker沒有填充散列表日期字段
我們使用STRUTS 1來映射我們的表單數據 - 對於我們大部分的數據來說,這就足夠了。
我已經檢查並寫過這些字段,以便可以通過手動輸入日期填充它們,現在我想添加jQuery datepicker以允許用戶從彈出日曆中選擇日期。我正在使用Chrome。下面的代碼片段。
<script>
$(function() {
$(".datePick").datepicker({
dateFormat: 'mm/dd/yy',
showOn: "button",
buttonImage: "images/calendar.png",
buttonImageOnly: true,
buttonText: "Select date",
onSelect: function(dateText,inst)
{
id=$(this).attr('id');
pos = id.indexOf("(") + 1;
counter = id.slice(pos, -1);
id=id.replace("("+counter+")","");
pieces = dateText.split("/");
$("#"+ id+"MM("+counter+")").val(pieces[0]);
$("#"+ id+"DD("+counter+")").val(pieces[1]);
$("#"+id +"YYYY("+counter+")").val(pieces[2]);
}
});
});
</script>
<!-- "counter" is an integer which keeps track of what iteration of the
hashmap is being accessed, propertyValue is a string that allows me to
dynamically change the property of each form-->
<tr>
<td align="right" colspan="1">
<label for="setOfObjectsStartMMDtH("+counter+")">
<%=label.makeLabel(XXXXForm.FORM_KEY_VALUE)%></label>
</td>
<td align="left" colspan="4">
<% propertyValue="setOfObjectsStartDtHMM("+counter+")";%>
<html:text property="<%=propertyValue%>" styleId="<%=propertyValue%>"
size="2" maxlength="2"/> /
<% propertyValue="setOfObjectsStartDtHDD("+counter+")";%>
<html:text property="<%=propertyValue%>" styleId="<%=propertyValue%>"
size="2" maxlength="2"/> /
<% propertyValue="setOfObjectsStartDtHYYYY("+counter+")";%>
<html:text property="<%=propertyValue%>" styleId="<%=propertyValue%>"
size="4" maxlength="4"/>
<% propertyValue="setOfObjectsStartDtH("+counter+")";%>
<html:hidden styleId="<%=propertyValue%>" property="<%=propertyValue%>"
styleClass="datePick"+counter></html:hidden>
這是我的getters和setters的樣子。
private final HashMap setOfObjects = new HashMap();
/**
* @param startDtH the setOfObjectsStartDtH to set
*/
public void setSetOfObjectsStartDtH(String key, String startDtH) {
setOfObjects obj = getSetOfObjects(key);
obj.setStartDtH(startDtH);
}
/**
* @return the startDtH
*/
public String getSetOfObjectsStartDtH(String key) {
if(setOfObjectss.containsKey(key)){
SetOfObjects obj = getSetOfObjects(key);
return obj.getStartDtH();
}
return "";
}
/**
* @param startDtHDD the startDtHDD to set
*/
public void setSetOfObjectsStartDtHDD(String key, String startDtHDD) {
SetOfObjects obj = getSetOfObjects(key);
obj.setStartDtHDD(startDtHDD);
}
/**
* @return the startDtHDD
*/
public String getSetOfObjectsStartDtHDD(String key) {
if(setOfObjectss.containsKey(key)){
SetOfObjects obj = getSetOfObjects(key);
return obj.getStartDtHDD();
}
return "";
}
/**
* @param startDtHMM the startDtHMM to set
*/
public void setSetOfObjectsStartDtHMM(String key, String startDtHMM) {
SetOfObjects obj = getSetOfObjects(key);
obj.setStartDtHMM(startDtHMM);
}
/**
* @return the startDtHMM
*/
public String getSetOfObjectsStartDtHMM(String key) {
if(setOfObjectss.containsKey(key)){
SetOfObjects obj = getSetOfObjects(key);
return obj.getStartDtHMM();
}
return "";
}
/**
* @param startDtHYYYY the startDtHYYYY to set
*/
public void setSetOfObjectsStartDtHYYYY(String key, String startDtHYYYY) {
SetOfObjects obj = getSetOfObjects(key);
obj.setStartDtHYYYY(startDtHYYYY);
}
/**
* @return the startDtHYYYY
*/
public String getSetOfObjectsStartDtHYYYY(String key) {
if(setOfObjectss.containsKey(key)){
SetOfObjects obj = getSetOfObjects(key);
return obj.getStartDtHYYYY();
}
return "";
}
private SetOfObjects getSetOfObjects(String key){
if(setOfObjects.containsKey(key))
return (SetOfObjects)setOfObjects.get(key);
SetOfObjects set = new SetOfObjects();
set.setId(Integer.parseInt(key));
setOfObjects.put(key, set);
return set;
}
而這裏的SetOfObjects的樣子:
public class SetOfObjects{
private String startDtH = "";
private String startDtHMM = "";
private String startDtHDD = "";
private String startDtHYYYY = "";
/**
* @param startDtH the startDtH to set
*/
public void setStartDtH(String startDtH) {
startDtH = startDtH;
}
/**
* @return the startDtH
*/
public String getStartDtH() {
return startDtH;
}
/**
* @param startDtHMM the startDtHMM to set
*/
public void setStartDtHMM(String startDtHMM) {
this.startDtHMM = startDtHMM;
}
/**
* @return the startDtHMM
*/
public String getStartDtHMM() {
return startDtHMM;
}
/**
* @param startDtHDD the startDtHDD to set
*/
public void setStartDtHDD(String startDtHDD) {
this.startDtHDD = startDtHDD;
}
/**
* @return the startDtHDD
*/
public String getStartDtHDD() {
return startDtHDD;
}
/**
* @param startDtHYYYY the startDtHYYYY to set
*/
public void setStartDtHYYYY(String startDtHYYYY) {
this.startDtHYYYY = startDtHYYYY;
}
/**
* @return the startDtHYYYY
*/
public String getStartDtHYYYY() {
return startDtHYYYY;
}
}
最後,這裏的生成的html。
<script>
$(function() {
$(".datePick").datepicker({
dateFormat: 'mm/dd/yy',
showOn: "button",
buttonImage: "images/calendar.png",
buttonImageOnly: true,
buttonText: "Select date",
onSelect: function(dateText,inst)
{
id=$(this).attr('id');
pos = id.indexOf("(") + 1;
counter = id.slice(pos, -1);
id=id.replace("("+counter+")","");
pieces = dateText.split("/");
$("#"+ id+"MM("+counter+")").val(pieces[0]);
$("#"+ id+"DD("+counter+")").val(pieces[1]);
$("#"+id +"YYYY("+counter+")").val(pieces[2]);
}
});
});
</script>
<tr>
<td align="right" colspan="1">
<label for="setOfObjectsStartMMDtH("+counter+")"><label class="normal">Set of Objects</label></label>
</td>
<td align="left" colspan="4">
<input type="text" name="setOfObjectsStartDtHMM(0)" maxlength="2" size="2" value="" id="setOfObjectsStartDtHMM(0)"> /
<input type="text" name="setOfObjectsStartDtHDD(0)" maxlength="2" size="2" value="" id="setOfObjectsStartDtHDD(0)"> /
<input type="text" name="setOfObjectsStartDtHYYYY(0)" maxlength="4" size="4" value="" id="setOfObjectsStartDtHYYYY(0)">
<input type="hidden" name="setOfObjectsStartDtH(0)" value="" class="datePick" id="setOfObjectsStartDtH(0)">
((注:我知道了「‘+專櫃+’」不創造一個適當的標籤 - 但解決這個沒有固定的日曆功能))
日曆本身帶有彈出沒問題,但是當我選擇一個日期時,頁面跳轉到頂部,並且日期字段沒有填充 - 頁面上的任何地方都沒有錯誤消息,並且日曆也不會消失,如通常在選擇日期之後。
什麼會導致日曆顯示爲jQuery datepicker,但不允許我分配日曆值?
這與Java有什麼關係? – Compass
@Compass請注意Java片段 - 儘管我想這不是特別重要。我將刪除該標籤。 – Zibbobz
在開發人員模式/工具的控制檯中,您是否收到任何錯誤? – dama