简体   繁体   中英

getting option tag text in jquery

Can someone help me get the text of my option tag because i always get blank when i do.

here is the html/php

<div class="form-group">
    <label for="sel1" id = "type_set">Webhook Type:</label>
    <select name="type" id = "selecting">
        <option value="" selected disabled hidden >Choose Tagging</option>
        <?php

        $disp = PDODatabase::Instance()->QueryAll("SELECT * FROM social_media" );

        foreach($disp as $data){
            if($data['_DELETED'] != 1){
                echo "<option data-med='".$data['SOCIAL_NAME']."'  id = 'soc' value='".$data['ID']."'>".$data['SOCIAL_NAME']."</option>";
            }
        }

    ?>
    </select>

</div>

here is the script:

jQuery(document).on('click','#soc', function(e){
  e.preventDefault();
  var med = $('#selecting:selected').text();
  alert(med);
  // var tod = ""+year+""+month+""+dat+""+ran+""+med+"";
  // document.getElementById("wname_set").value = tod;

});

thank you T_T

#selecting<select>的ID,您需要选择选定的选项。

var med = $('#selecting option:selected').text();

请试试:

$('#selecting option:selected').text();

如果您想获取该值,请选择以下值:

var med = $("#selecting option:selected").val();

console $('#selecting option:selected')。text())

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