简体   繁体   English

无法使 Javascript 功能正常工作

[英]Trouble getting Javascript function to work

ORIGINALLY;起初; I had a button trying to call a function but nested inside of a form, the button will trigger submit form event by default: The advice was to add a to the button tag and I was able to get past my original problem.我有一个按钮试图调用一个函数但嵌套在一个表单中,默认情况下该按钮将触发提交表单事件:建议是向按钮标签添加一个,我能够解决我原来的问题。

My new problem is:我的新问题是:

So I have these buttons:所以我有这些按钮: j

And I want to have these buttons trigger whether or not a date field is hidden or not.无论日期字段是否隐藏,我都想让这些按钮触发。

    else if (question.Expected_Answer_Type == "Date")
{
    <input type="button" value="Add a Date @ViewBag.QuestionID" onclick="makeDateVisible(#@ViewBag.QuestionID)">

    @Html.TextBoxFor(m => m.QuestionnaireAnswers[i].Answer, Model.Date.ToString("M/d/yyyy"), new { @class = "form-control date hidden", @id = ViewBag.QuestionID }) if (Model.QuestionnaireAnswers == null)

    {
        <script>
            $(document).ready(function () {
                    $('#@(ViewBag.QuestionID)')
                        .val("@(DateTime.Today.ToShortDateString())");
                }

            );
            function makeDateVisible(@ViewBag.QuestionID) {
                alert("Hello");
            } 

        </script>
    }
}

I can't seem to get the Alert inside the makeDateVisible to trigger.我似乎无法在 makeDateVisible 中触发警报。

inside the browser.... I get the error: Uncaught SyntaxError: Invalid or unexpected token and the red line appears at the end of this line:在浏览器中......我收到错误:未捕获的语法错误:无效或意外的令牌,红线出现在此行的末尾:

  <input type="button" value="Add a Date @ViewBag.QuestionID" onclick="makeDateVisible(#@ViewBag.QuestionID)">

Instead of a Button tag use an input tag, and add type=“button” to the tag and this should fix your issue.使用 input 标签代替 Button 标签,并将 type="button" 添加到标签中,这应该可以解决您的问题。 I linked a website for reference.我链接了一个网站供参考。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/button https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/button

You should pass #@ViewBag.QuestionID in quotes then it will be treated as string argument您应该在引号中传递 #@ViewBag.QuestionID 然后它将被视为字符串参数

Otherwise, it is treated as variable thus error is generated.否则,它被视为变量,从而产生错误。

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

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