gpt4 book ai didi

javascript - Jquery获取所选选项下拉列表的文本

转载 作者:行者123 更新时间:2023-12-03 08:23:34 25 4
gpt4 key购买 nike

我有一个 HTML 表单,我正在循环一些数字输入字段值以在页面底部显示总计。

我需要获取最近的下拉菜单的文本值(不是选项值)。

我的 HTML 如下:

<div class="standard-row wsheet-row input-row" id="5438">
<label class="ib">
<span class="label-stacked">Category</span>
<select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
<option value="5438">NON LABOUR</option>
</select>
</label>
<label class="ib">
<span class="label-stacked">Total</span>
<input class="inp inp110 data-addup-total inp-calculate" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="0.00" type="text">
</label>
</div>
<div class="standard-row wsheet-row input-row" id="5240">
<label class="ib">
<span class="label-stacked">Category</span>
<select class="inp inp220 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][sub_category][]" name="sub_category">
<option value="5240">1ST FIX CYLINDER</option>
</select>
</label>
<label class="ib">
<span class="label-stacked">Price</span>
<input class="inp inp110 inp-disabled" readonly="" data-myname="worksheet[*][plots][!][price][]" name="price" value="£40.00" type="text">
</label>
<label class="ib">
<span class="label-stacked">Number</span>
<input class="inp inp55 inp-calculate" data-myname="worksheet[*][plots][!][number_units][]" name="number_units" value="" type="text">
</label>
<label class="ib">
<span class="label-stacked">Total</span>
<input class="inp inp110 inp-disabled data-addup-total" readonly="" data-myname="worksheet[*][plots][!][total_cost][]" name="total_cost" value="£0.00" type="text">
</label>
</div>
<div class="wsheet-labour-total">LABOUR: £4.00</div>
<div class="wsheet-nonlabour-total">NON LABOUR: £4.00</div>
<div class="wsheet-final-total">TOTAL CLAIM: £4.00</div>

我用来尝试在选择之前获取选择的 Jquery:

$(parentRow).find('.data-addup-total').each(function() {
// get closest sub_category
var subCategory = $(this).closest('.inp220 option:selected').prev().text();
alert(subCategory);

price = $(this).val().replace(/[^\d.-]/g, '');
numtotal += parseFloat(price);
});

如果我进行控制台,我会得到:

Object { length: 0, prevObject: Object, context: <input.inp.inp110.data-addup-total.inp-calculate> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.inp-disabled.data-addup-total> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.data-addup-total.inp-calculate> } worksheet.js:158:1
Object { length: 0, prevObject: Object, context: <input.inp.inp110.inp-disabled.data-addup-total> } worksheet.js:158:1

最佳答案

DEMO

$('.data-addup-total').each(function () {
// get closest sub_category
var subCategory = $(this).parent().siblings().find('.inp220 option:selected').text();
alert(subCategory);
});

试试这个

关于javascript - Jquery获取所选选项下拉列表的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33648854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com