gpt4 book ai didi

javascript - 提交时仅清除 HTML 表单中的特定字段

转载 作者:行者123 更新时间:2023-11-28 15:19:17 24 4
gpt4 key购买 nike

我有以下 HTML 表单代码:

<div data-role="popup" id="-add" data-theme="c" class="ui-corner-all" data-dismissible="false" data-overlay-theme="a" style="min-width:550px;">
<div data-role="content"> <a href="#" data-rel="back" data-role="button" data-icon="delete" data-iconpos="notext" style="position:absolute;top:-20px;right:-15px;">Delete</a>

<form id="item-form" action="#" formaction="process" data-ajax="false">
<input type="hidden" name="type" value="hidden1" />
<input type="hidden" name="how" value="hidden2" />
<input type="hidden" name="loc" value="hidden3" />
<input type="hidden" name="itemtype" value="hidden4" />
<div align="left">
<h3> Add</h3>

</div>
<div class="popup-hrule"></div>
<div class="ui-grid-a ui-responsive">
<div class="ui-block-a">
<label for="location_name">Name</label>
<input type="text" name="location_name" id="location_name" value="">
</div>
<div class="ui-block-b" data-schema="types">
<label for="type_name">Type</label>
<select name="type_name" id="type_name">
<option data-item=".textarea:name; .value:id; .repeat:true"></option>
</select>
</div>
<div class="ui-block-a">
<label for="address">Address</label>
<input type="text" name="address" id="address" value="">
</div>
<div class="ui-block-b">
<label for="city">City</label>
<input type="text" name="city" id="city" value="">
</div>
<div class="ui-block-a">
<label for="state">State</label>
<input type="text" name="state" id="state" value="">
</div>
<div class="ui-block-b">
<label for="zip">Postal</label>
<input type="number" name="zip" id="zip" value="">
</div>
<div class="ui-block-a">
<label for="_lat">Latitude</label>
<input type="number" name="_lat" id="_lat" value="">
</div>
<div class="ui-block-b">
<label for="_lon">Longitude</label>
<input type="number" name="_lon" id="_lon" value="">
</div>
</div> <a data-role="button" data-inline="true" onclick="$(this).closest('[data-role=popup]').popup('close');">Cancel</a>
<a data-role="button" data-inline="true" data-theme="a" onclick="$(this).closest('form').submit();$(this).closest('form').find('input').val('');$(this).closest('[data-role=popup]').popup('close')">Add</a>

</form>
</div>
</div>

由于这是一个 ADD 表单,我想关闭表单并清除除顶部隐藏字段之外的所有字段,这些字段是告诉我必须如何处理数据的标志。当我执行代码时,它会清除表单中的所有数据,包括隐藏字段,因此第二次我无法添加新记录。

对我来说,最好的方法是完全关闭表单,这样我就不必删除表单中的字段,当我尝试再次打开表单时,这些字段将获取新数据。

最佳答案

您正在清除所有 input 元素:

$(this).closest('form').find('input').val('');

如果您只想清除其中一些,则需要专门针对您想要的那些。例如,如果您想清除所有非隐藏 input 元素:

$(this).closest('form').find('input[type!="hidden"]').val('');

最糟糕的情况是您必须将这一行代码转换为几行代码来标识您想要的元素。但只要有一些 jQuery 可选择的模式来识别您想要的模式,这就是您需要做的。

关于javascript - 提交时仅清除 HTML 表单中的特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32210467/

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