[英]MVC2 - passing C# code in javascript function on page
我有一個輸入按鈕,並且onclick我想觸發一個javascript函數並傳遞我的C#對象屬性。 它包含一個Ajax函數所需的ID。 我在正確轉義報價時遇到問題。
<td><input type="button" value='Contact query' onclick="sendQuery"'(<%=Model.AssociatedLists[0].Id.ToString()%>)'"/></td>
function sendQuery(param) {
alert(param);
}
這是在瀏覽器中形成html的方式:
<input onclick="sendQuery" type="button" '(e543cd31-7491-432e-b8a4-9718ad2c44e2)"="" value="Contact query"/>
我的按鈕沒有觸發,因為我的HTML格式不正確。
有什么幫助嗎?
這應該可以解決問題
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>
代替
<input type="button" value='Contact query' onclick="sendQuery"'(<%=Model.AssociatedLists[0].Id.ToString()%>)'"/>
采用
<input type="button" value='Contact query' onclick="sendQuery('<%=Model.AssociatedLists[0].Id.ToString()%>')"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.