gpt4 book ai didi

JavaScript 验证

转载 作者:行者123 更新时间:2023-12-02 20:09:50 29 4
gpt4 key购买 nike

我有一个表单,人们可以单击并向表单添加字段,并且我需要让它在单击时验证这些字段中的值。

假设我单击它两次并获取 2 个独立的字段集,我需要旋转 % 以确保它在保存时等于 100。

我已放入此函数以使其在保存时进行验证,并且一切正常,除非我编辑其中没有任何字段的不同表单。
我正在尝试查看最简单的方法来检查并查看字段是否在那里,以便我可以检查它们。

这是我的代码:

function audit()
{
var totalsum = 100;
$( '[name="rotation\\[\\]"]').each(
function()
{
totalsum -= ($(this).val() * 1);
}
)
if(totalsum > 0){
alert("Your rotation % does not equal 100");
return false;}
return true;}
<小时/>
$('.add_creative img').click(
function(){
$.post( '/json/ad_creative/',
{ action: "add_creative", this_brand_id: $("[name='this_brand_id']").val(), adtype: $( '.new_run [name="adtype"]').val()},
function(data){
if( data.result ){
$('.add_creative').append(data.html)
}
},
'json'
)

最佳答案

这是一个工作示例:http://jsfiddle.net/maniator/YW4Sv/

(如果您有一些工作代码,我可以将其扩展以满足您的需求)

JS:

function audit() {
var totalsum = 100;
$('.rotation').each(function() {
totalsum -= ($(this).val() * 1);
})
if (totalsum != 0) {
alert("Your rotation % does not equal 100");
return false;
}
return true;
}


$('.add').click(function(){
$(this).before($('<input>', {class: 'rotation', name:'rotation[]'}));
return false;
});

$('#theForm').submit(audit);

HTML:

<form id='theForm'>
<button class='add'>ADD FIELD</button><br/>
<button>SUBMIT</button>
</form>

关于JavaScript 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7070970/

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