如何获取HTML标记id的值并使用jQuery将其存储在变量中?

时间:2017-06-13 20:26:19

标签: javascript jquery jsp

这是我的jsp:

<h1>Drop Down Menu</h1>
                                <div class="dropdown">
                                <select name="doctype" id="${rep}" class="my_dropdown" style="color:white; background:#655D78">
                                    <option disabled="disabled" selected="selected">Select Doc Type</option>

这是我的jQuery函数:

<script type="text/javascript">
        $(document).ready(function() {
          var name = $("").val(); //value of the html id to be stored
          alert($.name);
            $.ajax({
             url:'',
             type:'post',
             cache:false,
             success: function(response){


             }
            });

        });


        </script>

我希望$ {rep}这个值在jQuery函数中。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

要获取select id的值(在您的情况下: $ {rep} ),您可以按attribute value选择元素:

$('select[name="doctype"]')

&#13;
&#13;
var name = $('select[name="doctype"]').attr('id');
console.log('name: ' + name);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<h1>Drop Down Menu</h1>

<div class="dropdown">
    <select name="doctype" id="${rep}" class="my_dropdown" style="color:white; background:#655D78">
        <option disabled="disabled" selected="selected">Select Doc Type</option>
    </select>
</div>
&#13;
&#13;
&#13;