简体   繁体   中英

How to set the value of the multiple input field from function in javascript

I can set or send value to the input field after some function. I can display my value instead of placeholder after doing some action as i need. It is working only for a single input filed, so how I can do it for two or more input field (input type is text) ? Working code is below,

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("input:text").val("Glenn Quagmire");

    });
});
</script>
</head>
<body>

<p>Name: <input type="text" name="user" placeholder="entername"></p>


<button>Set the value of the input field</button>

</body>
</html>

In this way I want to set the values for two input field after clicking button. How can i make it functional ?

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("input:text").val("Glenn Quagmire");
        $("input.text").val("Kathmandu");
    });
});
</script>
</head>
<body>

<p>Name: <input type="text" name="user" placeholder="entername"></p>
<p>Add : <input type="text" name="add" placeholder="address"></p>

<button>Set the value of the input field</button>

</body>
</html>

Thank you in advance.

You can set values for different input fields by using their name property.

$(document).ready(function(){
    $("button").click(function(){
        $('input[name="user"]').val("Glenn Quagmire");
        $('input[name="add"]').val("Kathmandu");
    });
});

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