1

我有一个asp:TextBox如下所示

<asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" ontextchanged="txtG1_TextChanged" onmouseout="javascript:RefreshIt(this)"/>

和一个 Javascript 函数RefreshIt(),每次我鼠标移出文本框时都会正确触发。

我试图让 mouseout 事件ontextchanged触发asp:TextBox.

各种 Stck 溢出帖子都推荐了以下技术,这些技术似乎不起作用:

function RefreshIt(selectObj){
 selectObj.ontextchanged();  
}

function RefreshIt(selectObj){
 selectObj.fireEvent('ontextchanged');  
}

任何帮助,将不胜感激。

4

3 回答 3

4

请参阅:https ://stackoverflow.com/a/3777/892536

使用此链接,我能够想出与您正在寻找的结果相同的东西。不确定这是否适合您的应用程序,但它有效:

ASP:

将 RefreshIt 函数更改为使用参数进行回发:

  <script type="text/javascript">
    function RefreshIt(selectObj) {
      __doPostBack('<%= Page.ClientID %>', selectObj.name);
    }
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>

    <asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" OnTextChanged="txtG1_TextChanged"
      onmouseout="javascript:RefreshIt(this);" />

    <br />
    <br />
    Text Changed:&nbsp;

    <asp:Label ID="Label1" runat="server"></asp:Label>

  </div>
  </form>
</body>

代码背后:

在页面中添加了“IPostBackEventHandler”并处理了“RaisePostBackEvent”函数:

public partial class _Default : System.Web.UI.Page, IPostBackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RaisePostBackEvent(string Arg)
    {
        if (txtG1.ID == Arg)
            txtG1_TextChanged(txtG1, null);
    }

    protected void txtG1_TextChanged(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
    }
}
于 2012-06-27T00:13:32.607 回答
1

//OnTextChanged in JavaScript works in IE,Firefox,Edge,Chohme,Safari,Opera
//My english is not that good, my mother tongue is geman

Html.:
<FORM action="../cgi-bin/`enter your file here (sendmail-anhang.pl)'" enctype="multipart/form-data" method="post" name="onlineform"> 
<INPUT type="hidden" name="OnFocus_start" value="0">
<INPUT type="hidden" name="OnFocus_startup_value" value="">
<INPUT type="text" name="Vorname" value="*" onKeyPress="if(event.keyCode == 13){;oent('0');return false}"><BR>
<INPUT type="text" name="Name"    value="*" onblur="BlurFunction('Myname')" onFocus="FocusFunction(document.onlineform.Name,this.value)" onkeyup="KeydUpFunction(document.onlineform.Name,this.value,'Myname')" onKeyPress="if(event.keyCode == 13){;oent('1');return false}"><BR>
<INPUT type="text" name="EMail"   value="*" onKeyPress="if(event.keyCode == 13){;oent('2');return false}"><BR>
<INPUT type="submit" value="Send Request">
</FORM>

<SCRIPT>
var stopwatches1

function stopwatch(Aktion) {
    
if(Aktion == "ende"){   
  var ende = window.sessionStorage.getItem('Mytime')
  window.sessionStorage.removeItem('Mytime')
  clearInterval(stopwatches1)
  return ende;
  }

if(Aktion == "weiter"){ 
var MyTime1 = window.sessionStorage.getItem('Mytime')
MyTime1++ 
var MyTime2 = MyTime1 +9
window.sessionStorage.setItem('Mytime',MyTime2) 
}
}

function FocusFunction(myField,myFieldvalue){
if(document.onlineform.OnFocus_start.value ==  0){
document.onlineform.OnFocus_start.value =  1
document.onlineform.OnFocus_startup_value.value = myFieldvalue
}
;}

function KeydUpFunction(myField,myFieldvalue,iput){
if(myFieldvalue != document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  1){   
document.onlineform.OnFocus_start.value =  2
window.sessionStorage.setItem(iput,new Date().getTime())
stopwatches1 = setInterval(function(){ stopwatch("weiter") }, 10);
}
else{;
if(myFieldvalue == document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  2){
document.onlineform.OnFocus_start.value =  1
clearInterval(stopwatches1)
alert("gleich")
};}
;}


function BlurFunction(myInput) {
document.onlineform.OnFocus_start.value =  0
document.onlineform.OnFocus_startup_value.value = ""
var field16 = stopwatch("ende");    
field16 = field16/1000
window.sessionStorage.removeItem(myInput)
alert("zz  " +field16)

if(field16 <= 0.10 ){
alert("You are Roboter")
}
else{
window.sessionStorage.setItem('Evt2' + myInput,myInput + 'OK2')
}   
}
</SCRIPT>

//OnTextChanged in JavaScript

Html.:
<FORM action="../cgi-bin/`enter code here`sendmail-anhang.pl" enctype="multipart/form-data" method="post" name="onlineform"> 
<INPUT type="hidden" name="OnFocus_start" value="0">
<INPUT type="hidden" name="OnFocus_startup_value" value="">
<INPUT type="text"   name="Name" value="*" onBlur="BlurFunction()" onFocus="FocusFunction(document.onlineform.Name,this.value)" onKeyUp="KeydUpFunction(document.onlineform.Name,this.value)" onKeyPress="if(event.keyCode == 13){;EnterFunction('1');return false}">
<INPUT type="submit" value="Send Request">
</FORM>

<SCRIPT>
function FocusFunction(myField,myFieldvalue){
if(document.onlineform.OnFocus_start.value ==  0){
document.onlineform.OnFocus_start.value =  1
document.onlineform.OnFocus_startup_value.value = myFieldvalue
};}

function KeydUpFunction(myField,myFieldvalue){
if(myFieldvalue != document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value ==  1){   
// Code... do it! 
// alert("unequal") ... "function alert()" it does not work!
}
else{
alert("equal")
};}

function BlurFunction() {
document.onlineform.OnFocus_start.value =  0
}

function EnterFunction(gezu){
document.onlineform.OnFocus_start.value =  0
docf1 =  document.onlineform.Name
docf2 =  document.onlineform.EMail
docf3 =  document.onlineform.Adresse
docf4 =  document.onlineform.Hnr
docf5 =  document.onlineform.plz
docf6 =  document.onlineform.Ort
docf7 =  document.onlineform.phone
docf8 =  document.onlineform.dieNachricht

var gezu4 = new Array(docf1,docf2,docf3,docf4,docf5,docf6,docf7,docf8)
gezu4[gezu].focus()

}
</SCRIPT>

于 2020-10-06T18:54:00.750 回答
0

为什么不简单地将文本框的 AutoPostBack 属性设置为 true ,它会在每次更改文本时自动回发,从而触发 textchanged 事件!

简单的

于 2013-02-18T16:28:26.763 回答