JQuery从Select Option中获取值不起作用

时间:2022-11-27 20:00:28

I dont know why my jquery code does not works to get value from select option.

我不知道为什么我的jquery代码无法从select选项中获取值。

I have created a function where when I click on "Add New Row" button then it will create a new row to my table.

我创建了一个函数,当我单击“添加新行”按钮时,它将为我的表创建一个新行。

Here's my JS code to add new row

这是我添加新行的JS代码

$(".tambah_sofa").on('click',function(){
    html = '<tr id="row_'+i+'">';
    html += '<td><button type="button" id="delete-button" data-row-delete="row_'+i+'">X</button></td>';
    html += '<td><select name="sofa[]" id="sofa"> <option value="'+sofa_rumah+'">Sofa rumah</option><option value="'+sofa_pejabat+'">Sofa Pejabat</option><option>Sofa Kedai</option><option>Tilam Tak Bujang</option> </select> </td>';
    html += '<td>X</td>';
    html += '<td><input type="number" name="quantity[]" id="quantity_'+i+'" value="1" disabled="disabled"></td>';
    html += '</tr>';
    sidebar = '<tr id="row_'+i+'">';
    sidebar += '<td><input style="width:50%;" type="text" id="price_sofa" value="" disabled="disabled"></td>';
    sidebar += '</tr>';
    $('#table_item').append(html);
    $('#table_sidebar').append(sidebar);
    i++;
});

Below is my code to get select option values into textbox :

下面是我在文本框中选择选项值的代码:

    $('#sofa').on('change', function () {
    $('#price_sofa').val(this.value);
    }).trigger('change');

I try to follow this JSfiddle demo : http://jsfiddle.net/JwB6z/2/

我尝试按照这个JSfiddle演示:http://jsfiddle.net/JwB6z/2/

The code is working but when I try implement to my "add new row" function, it's not working.

代码正在运行,但是当我尝试实现我的“添加新行”功能时,它无效。

2 个解决方案

#1


3  

I believe this is because any new row is not being recognised. I think you should bind the change to body...

我相信这是因为没有任何新行被识别。我认为你应该把改变绑定到身体......

example:

例:

$('body').on('change', '#sofa', function () {

#2


1  

you can always use this:

你可以随时使用这个:

$('#sofa').on('change', function () {
$('#price_sofa').find(':selected').val();
}).trigger('change');

EDIT: The above one is used to get the value you entered if you want to add the text and not value you can always put

编辑:上面的一个用于获取您输入的值,如果您想要添加文本而不是您可以随时添加的值

$('#price_sofa).find(':selected').text();

EDIT2: When you use $('#price_sofa').val(this.value); you are not taking the selected option and a you can see in the fiddle here that you added they didn't use the function that you used.

EDIT2:当你使用$('#price_sofa')。val(this.value);你没有选择所选的选项,你可以在这里看到你添加的它们没有使用你使用的功能。

Hope this helps

希望这可以帮助

#1


3  

I believe this is because any new row is not being recognised. I think you should bind the change to body...

我相信这是因为没有任何新行被识别。我认为你应该把改变绑定到身体......

example:

例:

$('body').on('change', '#sofa', function () {

#2


1  

you can always use this:

你可以随时使用这个:

$('#sofa').on('change', function () {
$('#price_sofa').find(':selected').val();
}).trigger('change');

EDIT: The above one is used to get the value you entered if you want to add the text and not value you can always put

编辑:上面的一个用于获取您输入的值,如果您想要添加文本而不是您可以随时添加的值

$('#price_sofa).find(':selected').text();

EDIT2: When you use $('#price_sofa').val(this.value); you are not taking the selected option and a you can see in the fiddle here that you added they didn't use the function that you used.

EDIT2:当你使用$('#price_sofa')。val(this.value);你没有选择所选的选项,你可以在这里看到你添加的它们没有使用你使用的功能。

Hope this helps

希望这可以帮助