0

我有一个视图和它的控制器。视图具有以下按钮(其被重复多次针对不同的变体):MVC View正在刷新按钮onclick

 <button type="button" id="@variant.id" onclick="return Select()" style="padding:10px 15px;width:100%;height:100%;margin-top:-10px;"> 
         <div class="col-md-4"> 
          <img width="100" height="100" style="padding:5px" src="/test/@variant.logo" title="@variant.name" /> 

         </div> 
         <div class="col-md-8"> 
          <h4>@variant.name</h4> 
         </div> 
     </button> 

这是它访问功能:

<script type="text/javascript"> 
     function Select() 
     {   
     alert("test"); 
     return false;   
     } 
    </script> 

对于函数调用后由于某些原因,在视图是总是被刷新。你知道我的代码可能有什么问题吗?

+0

你的代码应该工作,这是您的完整的代码? – Satpal

+0

按钮或子元素是否具有其他可能正在刷新的其他事件处理程序? –

+0

看起来好像你正在做任何事情使它在该代码片段中刷新。 –

回答

1

您应该使用阻止默认值来停止事件,另请参阅event.stopPropagation()。

在这里,我阻止了这个事件,也许你应该使用它,如果你的条件是错误的。

function select(e){ 
 
    e.preventDefault(); 
 
    alert('test'); 
 
    return false; 
 
}
<button type="button" id="@variant.id" onclick="select(event)" style="padding:10px 15px;width:100%;height:100%;margin-top:-10px;"> 
 
       <div class="col-md-4"> 
 
        <img width="100" height="100" style="padding:5px" src="/test/@variant.logo" title="@variant.name" /> 
 

 
       </div> 
 
       <div class="col-md-8"> 
 
        <h4>@variant.name</h4> 
 
       </div> 
 
</button>