如何在MVC ASP.net的弹出窗口中触发文本框值更改事件

how to fire an event on textbox value change from pop window in MVC ASP.net

本文关键字:文本 事件 窗口 MVC ASP net      更新时间:2023-09-26

我现在从弹出窗口返回一个值,我希望文本框值从弹出窗口更改,它应该将背景颜色更改为红色,否则不会反映任何更改。我做的是

<input  type="button" id="button1" value="button1" style="width:95%;font-family:Verdana;font-size:9px;" onclick="window.open( '<%= Url.Action( "PopUp", "Home"  ) %>' ,'popup','height=410','width=200','target=popup','center','front');" onfocus="if(document.getElementById('HiddenVal').value!=null||document.getElementById('HiddenVal').value!=''){txtbutton.value=document.getElementById('HiddenVal').value ; document.getElementById('HiddenVal').value='';}" />

其中txtbutton是文本框,button1是打开弹出窗口的按钮。如果文本框的值发生更改,我想更改文本框的颜色;否则它的背景应该是白色的。

从按钮中删除onfocus代码,然后将此代码放入js文件中。

$('#button1').focus(function(){
   if(document.getElementById('HiddenVal').value!=null&&document.getElementById('HiddenVal').value!=''){
        $('#txtbutton', window.parent.document).css("background-color","red"); //Assuming txtbutton is ID of your textbox
         document.getElementById('HiddenVal').value='';
   }
} );

你可以尝试的另一种方法是在父窗口中编写一个函数,然后从子窗口调用该函数,类似于

<script> 
function ifDoneChild(val) {   
 $('#txtbutton').css("background-color","red");
} 
</script> 

从子窗口调用此函数-

 $('#button1').focus(function(){
           if(document.getElementById('HiddenVal').value!=null&&document.getElementById('HiddenVal').value!=''){
               window.parent.ifDoneChild(); 
            }
     });