最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .find() inputs of multiple types - Stack Overflow

programmeradmin2浏览0评论

I'm using the following jQuery to clone a set of input fields (1 Select field, 2 Text fields and 1 Number field)

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text']").val("");
});

Is there any way I can extend this so the cloned number fields don't also contain the values.

I've tried

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text|number']").val("");
});

Which doesn't work.

I'm using the following jQuery to clone a set of input fields (1 Select field, 2 Text fields and 1 Number field)

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text']").val("");
});

Is there any way I can extend this so the cloned number fields don't also contain the values.

I've tried

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text|number']").val("");
});

Which doesn't work.

Share Improve this question asked Jan 4, 2016 at 14:31 LukeLuke 3,5616 gold badges40 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The selector in .find("input[type='text|number']") is incorrect. To select multiple elements, separate the selectors by ma.

.find("input[type='text'], input[type='number']")

See Multiple Selectors


To set empty string as value to all the input elements, use element selector.

.find('input').val('')

You must use a multiple element selector using , to separate each selector; like:

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text'], input[type='number']").val("");
});
发布评论

评论列表(0)

  1. 暂无评论