简体   繁体   中英

How can I pass HTML data in Javascript

I am new to HTML/JS and currently working to improve one of the existing code I have.

So the existing code declares a form and the data is used in JS for further processing. Now I want to hard code this values without creating a form and use the same JS for further action. I don't want to change JS as both the behaviors should co-exist. This is the way the code looks now -

<div class="form-group">
 <label>Frequency</label>
   <select class="form-control delivery" data-code="<?php echo $prid; ?>">
     <option value="">-- Please Select --</option>
     <option value="">Once</option>
     <option value="">Twice</option>
   </select>
</div>

<script>
  $(document).ready(function(){
     $('.delivery').change(function(){
        var planval = $(this).val();
        var prid = $(this).data('code');
     }
  }

How can I pass val and code value without actually creating form?

attr() Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. Get the value of an attribute for the first element in the set of matched elements.

<div class="form-group">
 <label>Frequency</label>
   <select class="form-control delivery" data-code="<?php echo $prid; ?>">
     <option value="">-- Please Select --</option>
     <option value="">Once</option>
     <option value="">Twice</option>
   </select>
</div>

<script>
  $(document).ready(function(){
     $('.delivery').change(function(){
        var planval = $(this).val();
        var prid = $(this).attr('data-code');
     }
  }
</script>

You can set id for select tag like 1 point.

now you can use this for same function like 2 and 3 point

 1. <select id = "selectID" class="form-control delivery">
 2.  var planval = $("#selectID").val();
 3.  var prid = $("#selectID").data('code');

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