javascript无法更新输入框的值[关闭]

I have some input box. Here is the HTML

<input name="dog_shipping_price"  class="input" id="shipping_price_<?php echo $id; ?>" value="<?php echo $shipping_price; ?>" />

In some javascript i am trying to update its value

$('.shipping_price_'+item.id).val(parseFloat(item.shipping_price));

i also tried

$('.dog_shipping_price').val(parseFloat(item.shipping_price));

i verified that item.id is the right value and item.shipping_price is also there with the following:

console.log(parseFloat(item.shipping_price));

when i run my code the input box value is not updated

In your example, as your element's IDs are unique, you should be referencing an ID, not a class, so you should use:

$('#shipping_price_'+item.id).val(parseFloat(item.shipping_price));

. is a class selector, # is an ID selector.

Change:

$('.shipping_price_'+item.id).val(parseFloat(item.shipping_price));

to:

$('#shipping_price_'+item.id).val(parseFloat(item.shipping_price));

Since you are updating the value using id not class you should use '#' not '.'.

When you are using . selector it is referring to the class with dog_shipping_price or so. If you want to use class selector target the class attribute in your element which is input in your case

$('.input').val(parseFloat(item.shipping_price));