如何在 asp.net 中使用 jQuery 时防止页面刷新



>我正在尝试在我的asp项目中实现一些jQuery,每次触发函数时都会导致页面刷新回其初始状态。我可以看到由jQuery触发的结果一瞬间,然后页面正在刷新,所以我知道jQuery很好。

有没有办法阻止这种情况。我尝试四处搜索,但注意到这确实解释了如何做到这一点,而且我对asp .net的世界真的很陌生。

我在想我以某种方式需要使用 .preventDefault(),但没有设法将任何东西串在一起。

她的代码基本上是将选项的值放入文本框中,并在单击选择时显示文本框

 <select id="ops">
   <option>Choose Property</option>
   <option value="one">one</option>
   <option value="two">two</option> 
  </select>
 <asp:Button ClientIDMode="Static" ID="select" runat="server" Text="Select"/>
<div id="mydiv">
<asp:TextBox ClientIDMode="Static" Enabled="false" ID="textbox" runat="server" CssClass="textEntry" />
</div>

<script type="text/javascript">
       $(document).ready(function () {
           $('#mydiv').hide();
           $('#ops').click(getOps);
       });
       function getOps() {
           var choice = $(this).val();
           $('#select').click(function () {
               $('#mydiv').show();
               $('#textbox').val(choice);
           });
       }
</script>

使用event.prevenDefault();

<script type="text/javascript">
       $(document).ready(function () {
           $('#mydiv').hide();
           $('#ops').click(getOps);
       });
       function getOps() {
           var choice = $(this).val();
           $('#select').click(function (event) {
                event.preventDefault(); // stops form sumission
               $('#mydiv').show();
               $('#textbox').val(choice);
           });
       }
</script>

最新更新