简体   繁体   English

将 onclick 与部分 HTML 按钮一起使用

[英]Using onclick with a partial HTML button

When using onclick in the markup without using a partial HTML element, the onclick function works, but when using the exact same markup in a partial, it does not execute the onclick function在不使用部分 HTML 元素的情况下在标记中使用 onclick 时,onclick 函数可以工作,但是在部分中使用完全相同的标记时,它不会执行 onclick 函数

Question: Is there a way to get the onclick function to work using a partial HTML element, or do you have to write the markup normally?问题:有没有办法让 onclick 函数使用部分 HTML 元素工作,或者您是否必须正常编写标记?

This is my partial Submit button:这是我的部分提交按钮:

    <input type="submit" value="Submit" class="button1" />

This is where is my JavaScript Confirm function:这是我的 JavaScript Confirm 函数的位置:

    function Confirm(message, okayMessage, cancelMessage) {
        var result = confirm(message);
        if (result == true) {
            alert(okayMessage);
        } else {
            alert(cancelMessage);
        }
    }

This is where I call the onclick function where I use the partial button and Confirm script:这是我调用 onclick 函数的地方,我使用部分按钮和确认脚本:

    <partial name="_SubmitButton" onclick="Confirm('Are you sure you want to delete this skill?', 'Skill has been deleted', 'Skill was NOT deleted')" />

This is the link at the bottom of my markup to the stylesheet (It definitely works):这是我的标记底部到样式表的链接(它绝对有效):

    @section Scripts{
        <script type="text/javascript" src="~/js/Popups.js"></script>
    }

Hi its all because of our old friend DOM.嗨,这一切都是因为我们的老朋友 DOM。

Say when you have a partial page like this it will be loaded with the main page and will be binded with the DOM only after that your partial view would be loaded.假设当您有这样的部分页面时,它将与主页面一起加载,并且只有在您的部分视图被加载后才会与 DOM 绑定。

This all happens because your partial button is not binded with DOM.这一切都是因为您的部分按钮未与 DOM 绑定。 Just do one thing open your console in browser and try to getElementByName('_SubmitButton');只需做一件事,在浏览器中打开控制台并尝试getElementByName('_SubmitButton');

You will get undefined the best option would be place your js inside partial page itself then i guess it should work你会得到 undefined 最好的选择是将你的js放在部分页面本身然后我想它应该可以工作

and also if you put it inside而且如果你把它放在里面

 @section Scripts{
        <script type="text/javascript" src="~/js/Popups.js"></script>
    }

部分标签助手用于服务器端代码执行,因此它不识别前端的确认功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM