gpt4 book ai didi

javascript - JQuery 函数仅自动填充第一个输入字段

转载 作者:行者123 更新时间:2023-12-02 17:51:52 25 4
gpt4 key购买 nike

我正在尝试获取一个表单,当用户在素材字段中输入信息时自动填写输入,然后将其插入使用 JQuery 的方程式中,然后答案应在 postQuantity 中输出。由于用户可以添加输入,因此表单被设置为每个素材都应带有相应的 postQuantity(由其后缀号表示)。因此,应该使用素材 2 来查找 postQuantity2 和 3 来查找 3,依此类推。问题在于,只有第一个字段会自动填充,并且每当更改不同的素材类别时,都不会发生任何情况。任何关于我出错的地方的见解以及如何修复它的建议将不胜感激!谢谢!这是一个 JSFiddle - http://jsfiddle.net/gv0029/TwH5n/

HTML:

    <form>
<fieldset id="fence">
<div class="inputFence">
<fieldset class="fenceDescripton">

<legend><strong>Fence Description</strong></legend>
<label>Footage<input name="footage_1" class="footage" /></label>
<select name="fenceHeight_1" class="fenceHeight">
<!--got rid of "select" from the value. not needed at all-->
<option value="">Select Fence Height</option>
<!--got rid of the ids completely. the numbers from the values are all you need-->
<option value="6">6 Ft.</option>
<option value="8">8 Ft.</option>
</select>
</fieldset>
<fieldset class="post">
<legend><strong>Post Type</strong>

</legend>
<label>Post Quantity:
<input type="postQuantity" name="postQuantity_1" class="postQuantity" value="" />
</label>
<select name="postMeasurements_1" class="postMeasurements">
<option value="">Select Post Measurements</option>
<option value="23/8 x .065 x 8">2 3/8 x .065 x 8</option>
<option value="23/8 x .095 x 8">23/8 x .095 x 8</option>
</select>
</fieldset>
</div>
</fieldset>
<div>
<input type="button" id="btnAddFence" value="Add Another Fence" />
<input type="button" id="btnDelFence" value="Remove Fence" />
</div>


</form>

JS:

//Quantity for Posts
$("[class^='footage']").bind('keypress keydown keyup change', function(){
var footage = parseFloat($(this).val(),10);
var total = '';
var parts = $(this).attr('name').split("_");
var fenceNumber = parts[1];

if(!isNaN(footage)){
total = Math.ceil(footage /7);
$(":input[name='postQuantity_" + fenceNumber + "'" + ']').val(total.toString());
} else {
$(":input[name='postQuantity_" + fenceNumber + "'" + ']').val("");
}
});

//Dynamic Fence Input Fields
$('#btnAddFence').click(function () {

// create the new element via clone()
var newElem = $('.inputFence:last').clone();

// insert the new element after the last "duplicable" input field
$('.inputFence:last').after(newElem);

// enable the "remove" button
$('#btnDelFence').removeAttr('disabled');

//get the input name and split into array (assuming your clone is always last)
var parts = $('.fenceHeight:last').attr('name').split("_");
//change the second element of the array to be one higher
parts[1]++;
//join back into a string and apply to the new element
$('.fenceHeight:last').attr('name', parts.join("_"));

//do the same for other two inputs
parts = $('.postQuantity:last').attr('name').split("_");
parts[1]++;
$('.postQuantity:last').attr('name', parts.join("_"));

parts = $('.postMeasurements:last').attr('name').split("_");
parts[1]++;
$('.postMeasurements:last').attr('name', parts.join("_"));

parts = $('.footage:last').attr('name').split("_");
parts[1]++;
$('.footage:last').attr('name', parts.join("_"));


// business rule: you can only add 5 names
//if (newNum == 5)
//$('#btnAdd').attr('disabled','disabled');
});

$('#btnDelFence').click(function () {
//remove the last inputFence
$('.inputFence:last').remove();

// if only one element remains, disable the "remove" button
if ($('.inputFence').length == 1) $('#btnDelFence').attr('disabled', 'disabled');
});

$('#btnDelFence').attr('disabled', 'disabled');

最佳答案

我想你想要这样的东西:

http://jsfiddle.net/TwH5n/3/

我更改的行是:

$(document.body).on('keydown', '[class^="footage"]'

这会将事件绑定(bind)到所有 future 的“[class^="footage"]”元素

理想情况下你不会在 body 上这样做,因为它效率不高。找到更接近的父代,或者在创建时将事件附加到每个克隆。

关于javascript - JQuery 函数仅自动填充第一个输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286839/

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