Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
Cómo pasar la variable en python al JavaScript

¿Cómo llevar la variable de python al JavaScript?

Primero, hago la variable aleatoria en python y luego la paso a index.html

 @app.route('/index', methods=['GET','POST']) def index(): value = random.randrange(8) return render_template('index.html', value = value)

Luego, obtengo el valor en html y obtengo el valor por entrada

 <div class="print">{{value}} is passed.</div> <input value=value type="number" id="myVariable"/>

A continuación, obtengo la variable en html para JavaScript.

 <script type='text/javascript'> alert(document.getElementById('myVariable').value)

Realmente quiero obtener la variable de python para mostrar las imágenes aleatorias en html usando JavaScript

 document.write("<img src='" + images[myVariable] + "'/></a><p><span>" + caption[myVariable] + "</span></p>");

Este es el último que quiero hacer usando este código fuente. Pero no funciona y el índice siempre es 0. ¿Cómo debo resolverlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está utilizando un generador de números aleatorios y 0 (cero) puede ser una salida válida de su generador a veces, por lo que puede ser difícil depurar el problema. Además, aparentemente te faltan un par de {{}} en tu <input> .

 <input value={{value}} type="number" id="myVariable"/>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!