简体   繁体   中英

How to use the variable in the setting of next javascript block in django template?

I need to use the just inputed email, which I can get in Javascript, to define the email for the <script> code. How can I do that in a Django template?

    <script>
      function getRadioValue(id) {
      var radioBtn = document.getElementById(id);
      var email = document.getElementById('email').value;
      // alert(email); //I get the email now.
      if (radioBtn.value == "1"){
        $("div[id^='div1']").hide();
      }
      if (radioBtn.value == "0"){
        $("div[id^='div1']").show();
      }
    }  
    </script>
    <div id="div1" style="display:none;">
      <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-description="Job Posting Fee ($25.00)"
        data-amount="2500"
        data-email= email     *****does not work.     
        > 
      </script> <!-- data-email = email -->

You problem is Javascript-specific. What you want to do is to change the mail of the user in the script when he select a radio button.
So you just need to update the date-email attribute just after your .show() on the div. It'll update the script every time the user click on the radio button which is equal to 1 .

<script>
  function getRadioValue(id) {
      var radioBtn = document.getElementById(id);
      var email = document.getElementById('email').value;
      var myDiv = $("div[id^='div1']"); // Do some jQuery optimization by querying the node once.
      if (radioBtn.value == "1"){
          myDiv.hide();
      }
      else {
          myDiv.show();
          $("script", myDiv).attr('data-email', email); // <- update mail field here
      }
}  
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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