克隆/附加后如何将所选选项重置为默认选择

时间:2018-10-13 20:14:30

标签: javascript jquery

下面的javascript代码正在克隆div,没有任何问题。 但是,当克隆div时,它不会重置组合框的默认选定选项。

我需要帮助,以便在代码中添加该部分,以便在克隆时将所选选项重置为默认值。

谢谢

<div id="container">
<div class="addNew">

    <select name="first"
    class="country form-control m-bootstrap-select m_selectpicker" 
    style="" data-live-search="true">
    <?php
    include ('inc/db.php');

    $trusql7    = "select * from yari";
    $trusql8    = $trudba->query($trusql7);
    while ($trusql9     = $trusql8->fetch_assoc()) {
    $trumcs_id = $trusql9['id'];
    $trumcename = $trusql9['ename'];
    $trumcaname = $trusql9['aname'];
    ?>
    <option value="<?php echo $trumcs_id; ?>">
    <?php echo $trumcename; ?>
    </option>
    <?php } ?>
    </select>
<input type="button" class="newField" value="New Field For Stuff" />
</div>
</div>

$('#container').on('click','.newField', function () {
    var newthing=$('div.addNew:first').clone().find('.newField').removeClass('newField').addClass('remove').val('Remove Field!').end();
    $('#container').append(newthing);
});

$('#container').on('click','.remove', function () {
   $(this).parent().remove();
});

$('#add').on('click', function () {
   var thing=$('div.addNew:first').clone();
   $('#container').append(thing);
});

1 个答案:

答案 0 :(得分:0)

find()上使用thing定位克隆的select,并在其上使用val()

$('#add').on('click', function () {
   var defaultVal = '';// whatever default should be
   var thing=$('div.addNew:first').clone();
   thing.find('select.country').val(defaultVal );//reset select
   $('#container').append(thing);
});