Javascript 如何使用JQuery填充文本字段

Javascript 如何使用JQuery填充文本字段,javascript,jquery,html,Javascript,Jquery,Html,我有以下部门: <div id="modalUpdate"> <div class="modal-content"> <div class="header"> <h2>Update Item in Your Shopping Cart</h2> </div> <div class="copy"> <lab

我有以下部门:

<div id="modalUpdate">
    <div class="modal-content">
        <div class="header">
            <h2>Update Item in Your Shopping Cart</h2>
        </div>
        <div class="copy">
            <label>Item:</label>
            <select id="itemChoice">
                <option value="Wine" selected>Wine</option>
                <option value="Shot">Shot</option>
                <option value="Beer">Beer</option>
            </select>
            <br /><span id="spanPrice"></span><br /><br />

            <label>Quantity:</label>
            <input type="text" id="quantity" name="quantity" placeholder="10">

            <label>Price:</label>
            <input type="text" id="price" name="price" placeholder="$10.00">

        </div>
        <div class="cf footer">
            <input type="button" value="Cancel" id="cancelUpdate"><input type="button" value="Update Item" id="updateTable">
        </div>
    </div>
    <div class="overlay"></div>
</div>
警报显示正确的值,但由于某些原因,字段未填充


如何修复代码以使其正常工作?

模态内容
是一个类,因此jquery中的选择器应如下所示:

$("#modalUpdate .modal-content .copy #itemChoice").val(values[0]);
也就是说,由于ID选择器只能选择单个元素(根据定义,ID在页面上必须是唯一的),因此您可以直接使用子ID:

$("#itemChoice").val(values[0]);
$("#spanPrice").text("Each: " + values[1]);
$("#quantity").val(values[2]);
$("#price").val(values[3]);

模态内容
是一个类,因此jquery中的选择器应如下所示:

$("#modalUpdate .modal-content .copy #itemChoice").val(values[0]);
也就是说,由于ID选择器只能选择单个元素(根据定义,ID在页面上必须是唯一的),因此您可以直接使用子ID:

$("#itemChoice").val(values[0]);
$("#spanPrice").text("Each: " + values[1]);
$("#quantity").val(values[2]);
$("#price").val(values[3]);

如果我不理解你的问题,这应该行得通

        $(function() {
            // Select all <select> tag inside div#modalUpdate and callback this function for eac of them
            $('div#modalUpdate select').each(function() {
                // alert the ID attribute for the current SELECT tag as well as it's VALUE
                alert('SELECT#' + $(this).attr('id') + ' = ' + $(this).val())
            });
            // Select all INPUT element inside div#modalUpdate having [type="texte"] and execute a callback function for each of them
            $('div#modalUpdate input[type="text"]').each(function() {
                if ('quantity' === $(this).attr('id')) {
                    // Check ID for the current matched element before running a specific action
                    $(this)// .actionIfTrue();
                }
                else if ('price' === $(this).attr('id')) {
                    // Same as first condition
                    $(this)// .actionIfTrue();
                }
            });
        });
$(函数(){
//选择div#modalUpdate中的所有标记,并为其中的每一个调用此函数
$('div#modalUpdate select')。每个(函数(){
//提醒当前选择标记的ID属性及其值
警报('SELECT#'+$(this.attr('id')+'='+$(this.val())
});
//选择div#modalUpdate中具有[type=“texte”]的所有输入元素,并为每个元素执行回调函数
$('div#modalUpdate输入[type=“text”]”)。每个(函数(){
if('quantity'==$(this.attr('id')){
//在运行特定操作之前,请检查当前匹配元素的ID
$(this)/.actionIfTrue();
}
else if('price'==$(this.attr('id')){
//与第一个条件相同
$(this)/.actionIfTrue();
}
});
});

如果我不理解你的问题,这应该行得通

        $(function() {
            // Select all <select> tag inside div#modalUpdate and callback this function for eac of them
            $('div#modalUpdate select').each(function() {
                // alert the ID attribute for the current SELECT tag as well as it's VALUE
                alert('SELECT#' + $(this).attr('id') + ' = ' + $(this).val())
            });
            // Select all INPUT element inside div#modalUpdate having [type="texte"] and execute a callback function for each of them
            $('div#modalUpdate input[type="text"]').each(function() {
                if ('quantity' === $(this).attr('id')) {
                    // Check ID for the current matched element before running a specific action
                    $(this)// .actionIfTrue();
                }
                else if ('price' === $(this).attr('id')) {
                    // Same as first condition
                    $(this)// .actionIfTrue();
                }
            });
        });
$(函数(){
//选择div#modalUpdate中的所有标记,并为其中的每一个调用此函数
$('div#modalUpdate select')。每个(函数(){
//提醒当前选择标记的ID属性及其值
警报('SELECT#'+$(this.attr('id')+'='+$(this.val())
});
//选择div#modalUpdate中具有[type=“texte”]的所有输入元素,并为每个元素执行回调函数
$('div#modalUpdate输入[type=“text”]”)。每个(函数(){
if('quantity'==$(this.attr('id')){
//在运行特定操作之前,请检查当前匹配元素的ID
$(this)/.actionIfTrue();
}
else if('price'==$(this.attr('id')){
//与第一个条件相同
$(this)/.actionIfTrue();
}
});
});

谢谢。我想我错过了。我必须这样做,因为我有另一个模式弹出窗口,具有不同的ID,但字段具有相同的ID。再次感谢。啊,我明白了。在这种情况下,您的第二个模态应该有一个不同的ID,但是您应该为元素使用类,因为同一页面上的重复ID(技术上)是无效的HTML,并且可能使正确选择元素变得困难。仅供参考。如果您的问题得到解决,请标记一个答案,以帮助其他有类似问题的人。谢谢,让我等了7分钟。我想我错过了。我必须这样做,因为我有另一个模式弹出窗口,具有不同的ID,但字段具有相同的ID。再次感谢。啊,我明白了。在这种情况下,您的第二个模态应该有一个不同的ID,但是您应该为元素使用类,因为同一页面上的重复ID(技术上)是无效的HTML,并且可能使正确选择元素变得困难。仅供参考。如果您的问题得到解决,请标记一个答案,以帮助其他有类似问题的人。谢谢,让我等了7分钟。我已经可以用@ KATSTEVENS方法完成了。你的代码很好,但是请考虑对你所做的改变做一个解释,这样将来读者就可以用这个问题和答案来解决他们自己的问题。谢谢。我已经可以用@ KATSTEVENS方法完成了。你的代码很好,但是请考虑添加一个你所做的改变的解释,这样未来读者可以使用这个问题和答案来解决他们自己的问题。