MVC2-在页面上javascript函数中传递C\35;代码
我有一个输入按钮,单击一次我想启动一个javascript函数并传递我的C#object属性。它包含一个我在Ajax函数中需要的ID。我在正确转义我的引用时遇到问题MVC2-在页面上javascript函数中传递C\35;代码,javascript,function,asp.net-mvc-2,escaping,Javascript,Function,Asp.net Mvc 2,Escaping,我有一个输入按钮,单击一次我想启动一个javascript函数并传递我的C#object属性。它包含一个我在Ajax函数中需要的ID。我在正确转义我的引用时遇到问题 <td><input type="button" value='Contact query' onclick="sendQuery"'(<%=Model.AssociatedLists[0].Id.ToString()%>)'"/></td> function sendQuery
<td><input type="button" value='Contact query' onclick="sendQuery"'(<%=Model.AssociatedLists[0].Id.ToString()%>)'"/></td>
function sendQuery(param) {
alert(param);
}
我的按钮没有启动,因为我的HTML格式不正确
有什么需要帮忙的吗?这应该可以
onclick="sendQuery('<%=Model.AssociatedLists[0].Id.ToString()%>')"
onclick=“sendQuery(“”)”
而不是
<input type="button" value='Contact query' onclick="sendQuery"'(<%=Model.AssociatedLists[0].Id.ToString()%>)'"/>
删除onclick
引用,在按钮上添加id,然后尝试以下操作:
<script type="text/javascript">
$(document).ready(function () {
$('#your-button-id').click(function () {
sendQuery(<%=Model.AssociatedLists[0].Id.ToString() %>);
});
});
</script>
$(文档).ready(函数(){
$(“#您的按钮id”)。单击(函数(){
sendQuery();
});
});
谢谢,这很有效。我很困惑,因为visual studio在有(‘and’)的地方给了我红色的swiggly。但在浏览器中它可以工作。也许我遗漏了一些东西,但除了为我的答案添加上下文之外……你还添加了什么吗?对我来说,这似乎只是一个副本,更糟糕的是,它被认为是正确的答案。
<input type="button" value='Contact query' onclick="sendQuery('<%=Model.AssociatedLists[0].Id.ToString()%>')"/>
<script type="text/javascript">
$(document).ready(function () {
$('#your-button-id').click(function () {
sendQuery(<%=Model.AssociatedLists[0].Id.ToString() %>);
});
});
</script>