简体   繁体   中英

How can I specify multiple selector jquery

This is my html code:

<input type="text" id="home">
<input type"text" id="dog">

I need to do something to read the values in these fields. So I wrote my jquery code:

//this code is wrong because it doesn't work
$('input[type="text"][id="home"] input[type="text"][id="dog"]')....

How I can select something like this? Can anyone help me?

For selecting multiple items use comma in between them

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

    $(document).ready(function () {

        $('#home, #dog').val("hello");
    });
</script>
</head>

<body>

 <input type="text" id="home">
<input type"text" id="dog">
</body>

</html>

Use a comma between two selectors when you are selecting based on multiple criteria. A simple example is:

$("#element1, #element2, #element3")

Your example:

$('input[type="text"][id="home"], input[type="text"][id="dog"]')

See Multiple Selector (“selector1, selector2, selectorN”)

您应该使用$('#home, #dog');

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