猿问

页面上的动态内容未显示

我正在尝试使用 js 的 onclick 事件在页面上生成动态内容,但控制台中出现错误“Uncaught SyntaxError: Unexpected identifier”


html


{% load static %}

<head>

      <script type="text/javascript">

      function myFunction() {

        // document.getElementById("demo").innerHTML = "Paragraph changed.";

        document.getElementById("demo").innerHTML = "{{ Malegender }}";

      }

      </script>

  </head>


  <body>

    <div class="container mt-5">

<h1>Gender</h1>

      <hr>

      <h5>{{ mygender.as_p }}</h5>

      <h1 id="demo">Hello</h1>

      <div class="container">

        <button type="submit" name="gender_submit" class="btn btn-success" onclick="myFunction()">Confirm</button>

      </div>

    </div>

  </body>

视图.py


def Gender(request):

    gender_selection = GenderForm()

    male_detail = MaleForm()

    Male = False

    print("Value of male",Male)

    if 'gender_submit' in request.POST:

        male_detail = MaleForm(data=request.POST)

        Male = True

        print("Value of male d",Male)


    print("Value of male a",Male)

    return render(request, 'Gender.html',{"mygender":gender_selection,"Malegender":male_detail})


表格.py


class MaleForm(forms.ModelForm):

    class Meta:

        model = GenderModel

        fields = ('genderMale','genderMale1','genderMale2','genderMale3','genderMale4')

模型.py


from django.db import models

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver

# Create your models here.


class GenderModel(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)


    gender_choice=(('Male','Male'),('Female','Female'),)

    gender = models.CharField(max_length=6,choices=gender_choice,null=True,)

    genderMale = models.BooleanField(default=False)

    genderMale1 = models.BooleanField(default=False)

    genderMale2 = models.BooleanField(default=False)

    genderMale3 = models.BooleanField(default=False)

    genderMale4 = models.BooleanField(default=False)



当我单击按钮时,控制台上显示错误。当用户单击按钮时,我试图显示该内容


胡子哥哥
浏览 174回答 1
1回答

猛跑小猪

这个问题表明永远不要使用该onclick属性。相反,您可以尝试使用这样的事件侦听器:<button type="submit" name="gender_submit" class="btn btn-success"id="gender_submit_button">Confirm</button><script type="text/javascript">function myFunction() {&nbsp; document.getElementById("demo").innerHTML = "{{ Malegender }}";}document.getElementById('gender_submit_button').addEventListener('click', myFunction);</script>此外,请确保您的脚本放置后的按钮和演示。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答