如何 select tr sibling 并在 table 上输入
How to select tr sibling and input on table
我尝试 select 并在 select 产品和 return 尊重输入兄弟 tr 的价格之后获得价格。
我试试
function getPrice() {
$(this).closest('tr').find('input').val($(this).val());
};
和 nth-child()
的变体,但我不能。
我需要更新价格
<!-- Django template tags -->
{% for item in items %}
<tr>
<td>
<select name="product" class="form-control" onchange="getPrice()">
<!-- Django template tags -->
{% for product in products %}
<option value="{{product.pk}}">{{ product.title }}</option>
{% endfor %}
</select>
</td>
<td>
<input name="price" class="form-control price" type="number" step="0.01">
</td>
<td>
<input name="quantity" class="form-control" type="number" step="0.01">
</td>
<td>
<span class="span-is-link no" onclick="removeRow()">
<i class="fa fa-close"></i>
</span>
</td>
</tr>
{% endfor %}
<script>
function getPrice() {
// Initial test with get value of select.
// jQuery
// inpuy in equivalent line of tr
$(this).closest('tr').find('input').val($(this).val());
}
</script>
您的 getPrice()
函数不起作用,因为当您通过 onchange
HTML 属性调用它时,this
关键字将是 window
,即浏览器全局对象,而不是您的 <select>
元素。
一个解决方案是用 getPrice.call(this)
替换 HTML onchange
属性中的 getPrice()
。这样,函数中的 this
将是 <select>
.
<select name="product" class="form-control" onchange="getPrice.call(this)">
我尝试 select 并在 select 产品和 return 尊重输入兄弟 tr 的价格之后获得价格。
我试试
function getPrice() {
$(this).closest('tr').find('input').val($(this).val());
};
和 nth-child()
的变体,但我不能。
我需要更新价格
<!-- Django template tags -->
{% for item in items %}
<tr>
<td>
<select name="product" class="form-control" onchange="getPrice()">
<!-- Django template tags -->
{% for product in products %}
<option value="{{product.pk}}">{{ product.title }}</option>
{% endfor %}
</select>
</td>
<td>
<input name="price" class="form-control price" type="number" step="0.01">
</td>
<td>
<input name="quantity" class="form-control" type="number" step="0.01">
</td>
<td>
<span class="span-is-link no" onclick="removeRow()">
<i class="fa fa-close"></i>
</span>
</td>
</tr>
{% endfor %}
<script>
function getPrice() {
// Initial test with get value of select.
// jQuery
// inpuy in equivalent line of tr
$(this).closest('tr').find('input').val($(this).val());
}
</script>
您的 getPrice()
函数不起作用,因为当您通过 onchange
HTML 属性调用它时,this
关键字将是 window
,即浏览器全局对象,而不是您的 <select>
元素。
一个解决方案是用 getPrice.call(this)
替换 HTML onchange
属性中的 getPrice()
。这样,函数中的 this
将是 <select>
.
<select name="product" class="form-control" onchange="getPrice.call(this)">