简体   繁体   中英

getting input box value undefined in alert box

I want to get input field value. Only based on class and id name. I am getting undefined in alert box. Not getting any value which i have entered in input box.

HTML

 <div id="tab1" class="tab-pane active">
        <div class="input-text">
            <div class="input-append">
                <input id="textData" class="input-xxlarge" type="text" name="inputName" />
                <button id="pressme" class="btn btn-success btn-large" type="button">Convert</button>
            </div>
        </div>
    </div>

JS

 $('#pressme').on({
     'click': function () {
         var data = $(".tab-pane active #textData").val();
         alert(data);

     }
 });

.tab-pane active looks for an <active> element inside of an element with a class of tab-pane . The correct selector for an element with those two classes would be .tab-pane.active ., but since your element has an id, just select it using an id selector:

$("#textData").val();

您可以将选择器用作您的文本框ID

var data = $("#textData").val();

Your selector is incorrect as the div has both classes, so you need to join them together like this:

var data = $(".tab-pane.active #textData").val();

Note that id selectors should be unique, so in effect the class selector is redundant anyway.

It will be like

$('#pressme').on({
     'click': function () {
         var data = $(".tab-pane.active #textData").val();
         alert(data);
     }
});

Makesure that you have enclosed it on DOM ready

TRY THIS:

 $('#pressme').on('click',function () {
         var data = $("#textData").val();
         alert(data);

     });

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