2011-12-19 78 views
6

我的下面的代碼在HTML中使用時工作正常。我試圖把它放在一個表單中並停止工作。如何解決這個問題?jQuery不能與runat =「server」一起工作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<script src="jquery-1.7.1.min.js" ></script> 
<title>Chat scroll test</title> 
<style type="text/css"> 
#chat 
{ 
    width: 200px; 
    height: 200px; 
    border: 1px solid black; 
    overflow-y: auto; 
} 
</style> 
<script> 
    jQuery(document).ready(function ($) { 
     monitor = function() { 
      var $this = $(this), 
     wrap = $this.find('.wrapper'), 
     height = $this.height(), 
     maxScroll = wrap.height() - height, 
     top = $this.scrollTop(); 
      if (maxScroll === top) { 
       $this.addClass('atBottom'); 
      } else { 
       $this.removeClass('atBottom'); 
      } 
     } 
     window.setInterval(function() { 
      monitor.call($('#chat').get(0)); 
     }, 350); 
     $('#chat').bind('addMessage', function (e, message) { 
      var $this = $(this), 
     top = $this.scrollTop(), 
     scroll = $this.hasClass('atBottom'); 
      $this.find('.wrapper').append(message); 
      if (scroll) { 
       var wrap = $this.find('.wrapper'), 
      height = $this.height(), 
      maxScroll = wrap.height() - height 
       $this.scrollTop(maxScroll); 
      } 
     }) 
     $('button').click(function() { 
      $('#chat').trigger('addMessage', 'asdgagasdg<br/>'); 
     }); 
    }); 
</script> 
</head> 

<body> 
<form id="form1" runat="server"> 
<div id="chat"> 
    <div class="wrapper"> 
     adsgasgda<br/> 
     adsgasg<br/> 
     asd<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
     adsgasgda<br/> 
     adsgasg<br/> 
    </div> 
</div> 
<button>Add a line</button> 
</form> 
</body> 
</html> 

當表格標籤被刪除後,它又重新開始工作。

+0

? – Chris 2011-12-19 15:13:25

+0

你用什麼.net版本? – 2011-12-19 15:17:02

+0

是啊!當打開時沒有

標籤,一切工作正常。 – 2011-12-19 15:20:13

回答

2

當你有一個裏面自動提交表單。 與

<input type="button" value="Add a line" /> 

和方式單擊時,瀏覽器不會發布形式更換

<button>Add a line</button> 

也請記住,如果你看一下HTML瀏覽器的所有元素的ID,你期望他們從

$('button').click(function() { 

jQuery選擇更改爲類似

$(':button').click(function() { 
+0

這工作!謝謝安德烈斯。 – 2011-12-19 16:56:26

2

提示:查看最終HTML代碼的ID。

這應該讓你知道發生了什麼。對此有許多解決方案,但這取決於您使用的是哪個版本的.net。

+0

我正在使用.NET 4.0,不知道什麼是MVC。 – 2011-12-19 15:18:09

6

對於.NET 4.0,添加ClientIDMode Static用於標籤出現在客戶端,與它們出現在服務器端相同。

喜歡的東西

    <form id="form1" runat="server" ClientIDMode="Static"> 
在你的jQuery/Javascript代碼

對於老.NET中使用 '<%= Control.ClientID%>' 每當你指的控制

喜歡的東西

    '<%= form1.ClientID %>' 
+0

這對我沒用Emmanuel :( – 2011-12-19 15:31:32

+0

請問你可以發佈通過運行你的代碼生成的HTML。你添加了什麼標籤'ClientIDMode =「Static」'? – 2011-12-19 15:33:55

+0

通過添加'ClientIDMode =「Static」'我得到了當你的系統沒有放置的時候,我得到的輸出是相同的! – 2011-12-19 16:45:44