1

i have a form that has an input field that uses a datalist, what i want to do is when something is selected from that field, update another input field with what was selected, here is what i have so far

                <div class='form-row'>
                    <div class='col-xs-12 form-group required'>
                        <h2><label class='control-label'>Product code</label></h2>
                        <input id="ItemSelect" type=text list=jobs style='font-size:21px;height: 40px;'
                               class='form-control'
                               name="Code">
                        <datalist id=jobs>
                            <?php
                            foreach ($this->Products as $a) {
                                echo '<option value=' . $a["Code"] . '>' . $a["Code"] . " - " . $a["Name"] . ' </option>';
                            }
                            ?>
                        </datalist>
                    </div>

                    <div class='col-xs-12 form-group required'>
                        <input id="Placeholder" readonly class='form-control'
                               placeholder="Prodcut Descirption">
                    </div>

                    <script>
                        $("#ItemSelect")
                            .change(function () {
                                var str = "";
                                $("datalist option:selected").each(function () {
                                    str += $(this).text() + " ";
                                });
                                $("#Placeholder").attr("placeholder", str).val("").focus().blur();
                            })
                            .change();

                    </script>

this kind of works, it is selecting the right input field to detect the change, however it's not putting the value in. if i change

$("datalist option:selected") 

to

$("select option:selected")

it does put the value of all the other select fields in there any ideas?

4

2 回答 2

2

数据列表中没有这样的东西option:selected

您可以通过查看当前的内容来获取所选值input

$("#ItemSelect").change(function() {
    var str = $(this).val() + " ";
    $("#Placeholder").attr("placeholder", str).val("").focus().blur();
});

如果您只想在输入值实际存在于数据列表中时执行此操作:

$("#ItemSelect").change(function() {
    var str = $(this).val() + " ";.

    $('datalist option').each(function() {
        if($(this).text() === str) {
            $("#Placeholder").attr("placeholder", str).val("").focus().blur();
        }
    }); 
});
于 2015-04-30T19:33:45.667 回答
-1
<datalist id=jobs>

should change datalist tag to select tag

<select id=jobs>

once you change that the below statement will work

$("select option:selected") 
于 2015-03-17T16:03:15.830 回答