頁面中:
<script type="text/javascript">
//建立ajax對象
var ajax =new function()
{
this._url = "updateSimilarity!updateSimilarity.action";
//用於建立XMLHttpRequest對象
this.createXmlHttp=function() {
var xmlHttp = null;
//根據window.XMLHttpRequest對象是否存在使用不同的建立方式
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
return xmlHttp;
}
//修改相似性
this.updateSimilarity = function(rownum)
{
var rownum = rownum;
var similarity = document.getElementById("similarity"+rownum).value.trim(); //文字框的值
var keywordscategid = document.getElementById("keywordscategid"+rownum).value.trim();
var actiontype = document.getElementById("actiontype"+rownum).value.trim();
res = /^([1-9][0-9]*|0)$/;
if(similarity == ""){
alert("相似性不可為空!");
document.getElementById("similarity"+rownum).focus();
return false;
}
if(!(res.test(similarity))){
alert("相似性,請輸入正整數,不能含有特殊字元!");
document.getElementById("similarity"+rownum).focus();
return false;
}
var xmlHttp=this.createXmlHttp(); //建立XmlHttpRequest對象
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200){
alert("修改成功!");
window.parent.frames.mainFrame.location.reload();
} else{
alert("伺服器故障");
}
}
}
var url=this._url + "?similarity="+ similarity +"&keywordscategid="+keywordscategid+"&actiontype="+actiontype;
xmlHttp.open("GET",encodeURI(encodeURI(url)), true);
xmlHttp.send(null);
}
}
</script>
<input type="text" name="similarity" id="similarity<%=j%>" value="<s:property value="similarity"/>" size="3" maxlength="3" class="biaoti2"/>
<a href="#" onclick="ajax.updateSimilarity(<%=j%>);">修改</a>