1.直接點出該控件的屬性值 進行修改
使用JS來改變INPUT控件的屬性(value、disabled等)
document.getElementById('ID').disabled=false;
document.getElementById('ID').title="12";
document.getElementById('ID')..style.borderColor = "#FF0000";
大家看看就明白了,可是要是在gridview里面怎么獲取我要的控件的ID呢?
方法一:后台綁定事件中
在GridView的RowDataBound事件中
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((LinkButton)e.Row.Cells[4].FindControl("lbupdate")).Attributes.Add("onclick", "return Show('" + ((TextBox)e.Row.Cells[2].FindControl
("defpwd")).ClientID + "')");
}
前台JS函數:
function Show(did) //did 是后台傳遞過來的參數,為控件ID
{
var defpwd=document.getElementById(did);
if(defpwd.value.length<=0)
{
alert('不能為空');
defpwd.focus();
return false;
}
}
方法二:利用JS在前台獲取
前台函數
function Check(e)
{
var did; //將要獲取控件的ID
var el= e.target?e.target:e.srcElement;//這里是兼容FF和IE獲取event
var r=el.parentElement.parentElement.rowIndex +1;//獲取元素所在的行的行號(只兼容IE)
// 上面換成 var r=el.parentNode.parentNode.rowIndex +1;//兼容FF與IE
if(parseInt(r)<10) // 這里要判斷r的行號,因為GridView行號自動在小於10之前加0
{
did = 'GVadmin_ctl0' + r + '_defpwd';//獲取TextBox的ID GVadmin_ctl02_defpwd
}
else
{
did = 'GVadmin_ctl' + r + '_defpwd';//獲取TextBox的ID GVadmin_ctl02_defpwd
}
var odbj = document.getElementById(did);
if(odbj.value.length<=0)
{
alert('不能為空!');
odbj.focus();
return false;
}
}
用JavaScript獲取Gridview中某個觸發事件控件的ID
gridview獲取當前觸發控件的id window.event.srcElement.id