使用Jquery为不同的多个文本框设置不同的多个值
我正在创建一个脚本,管理员可以添加所有国内地区的费用,也可以添加国际地区的费用。情况是:- 管理员点击添加费用按钮。在那里,他可以增加费用、成本和交货天数。现在我想为所有具有使用Jquery为不同的多个文本框设置不同的多个值,jquery,Jquery,我正在创建一个脚本,管理员可以添加所有国内地区的费用,也可以添加国际地区的费用。情况是:- 管理员点击添加费用按钮。在那里,他可以增加费用、成本和交货天数。现在我想为所有具有name=delivery\u charge[][charge]的文本框设置charge的值,为所有具有name=delivery\u charge[][cost]的文本框设置delivery\u days的值,并为所有具有name=delivery\u charge[][days]的文本框设置的值 我所做的是 functi
name=delivery\u charge[][charge]
的文本框设置charge
的值,为所有具有name=delivery\u charge[][cost]
的文本框设置delivery\u days
的值,并为所有具有name=delivery\u charge[][days]的文本框设置的值
我所做的是
function addCharges(type){
var cost = parseFloat($('#modal-cost').val());
var charge = parseFloat($('#modal-charge-box').val());
var days = parseInt($('#modal-days').val());
if(isNaN(cost)){
cost = 0;
}
if(isNaN(charge)){
charge = 0;
}
if(isNaN(days)){
days = 0;
}
if(type == 'domestic'){
$('.domesticZones :checkbox').prop("checked", true);
$('.domesticZones input').prop("readonly", false);
//What code should i put here so that all text box with name delivery_charge[][cost] have value of cost and same for variables charge and days.
}else if(type == 'international'){
$('.internationalZones :checkbox').prop("checked", true);
}
}
使用以下命令:
// find name with [cost] at the end of name
$('input[name$="[cost]"]').val(cost);
// same goes to the others
更新的注释应该可以做到这一点,您也可以使用regex尝试此解决方案,但与上面的代码相比,这需要相当多的新行:
$('input[name$="[cost]"]').filter(function(){
// find the attribute name start with `delivery_charge`
// name and not followed by `_`
var regex = /delivery_charge(?!\_)/;
if ( regex.test( $(this).attr('name') ) ) return this;
}).val(cost);
阅读此内容并查看此内容。您能否发布由php脚本生成的相关HTML代码它可以工作,但它也会更改具有name=delivery\u charge\u international[][days]
*[cost]
未天数的文本框的值。使用此$('.domesticZones输入[name$=”[cost]')。val(cost);
。很高兴听到这个消息