简体   繁体   中英

how to get value of these inputs using javascript or jquery

I was wondering if it possible to find an input by it's closest element? real code:

<form method="post" action="">
    <div class="rocket-form">
        <fieldset>
            <legend>Person Contacts</legend>
        </fieldset>

        <div class="rocket-label-element no-height">
            <dt id="id-label">&nbsp;</dt>
            <dd id="id-element">
                <input type="hidden" name="id" value="" readonly="readonly" id="id">
            </dd>
        </div>
        <div class="rocket-label-element no-height"><dt id="vendor_id-label">&nbsp;</dt>
            <dd id="vendor_id-element">
                <input type="hidden" name="vendor_id" value="" readonly="readonly" id="vendor_id">
            </dd>
        </div>
        <div class="rocket-label-element">
            <div id="firstname-label">
                <label for="firstname" class="rocket-label optional">First name</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="firstname" id="firstname" value="any first name" maxlength="64" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="lastname-label">
                <label for="lastname" class="rocket-label optional">Last name</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="lastname" id="lastname" value="any last name" maxlength="64" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="phone-label">
                <label for="phone" class="rocket-label optional">Phone</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="phone" id="phone" value="0123456789" maxlength="32" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="email-label">
                <label for="email" class="rocket-label optional">Email</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="email" id="email" value="name@someMail.com" maxlength="128" readonly="readonly">
            </div>
        </div>
    </div>
</form>

I have this form, and I want to get the value of the inputs in variables.. I tried to use "vendor_id" in order to use closest() or next() or prev() but no luck.. so, any help?

You can use

var vendorId = $("#vendor_id").val();

to get the value of vendor_id.

Be sure to include the # which identifies it as an id.

If you want to get the values of all the inputs, you can use:

$("input, select, textarea").each(function(){
    // Get value of inputs
});

If you have more than one form, you may use:

var formData = $(".rocket-form").closest("form").serialize();

Remember you will need to include jQuery with a script tag and you should wrap your code like so:

$(function() {
    console.log( "ready!" );
});

This ensures the page is ready before your JavaScript executes.

不知道我是否完全理解您的要求,但那又如何

$('#vendor_id-element input').val()

try this

var inputs = $('form').find('input');
//inputs is the array of your inputs and values can be accessed by:
var value1 = $(inputs[0]).val();

//or you can use for loop or forEach
for (var i = 0; i < inputs.length; i++) {
    var currentValue = $(inputs[i]).val();
    alert(currentValue);

}

You a class in all your inputs where you want to get value, and do de following js

  Array.prototype.slice.apply(document.querySelector('.your-input-class')).forEach(function(el){
       Console.log(el.value);
    });

you can try this

var form = $("#vendor_id").closest("form").get();
$("form :input").each(function(){
 var input = $(this);
 var AllInputValues= $(input).val();
 alert (AllInputValues);

});

EDIT

var theVendform = $("#vendor_id").parent().closest("form").get();
                alert(theVendform["0"][3].defaultValue);
                alert(theVendform["0"][4].defaultValue);
                alert(theVendform["0"][5].defaultValue);
                alert(theVendform["0"][6].defaultValue);

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