Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

javascript - 下拉附加表单提交两次(Dropdown appends form submit twice)

EDIT: solved with a count variable and if/else.(编辑:用一个计数变量和if / else解决。)

Not the most elegant but it works.(不是最优雅的,但它可以工作。) Would be interesting to know why it happens / how it should be solved though so i'll keep this here.(知道为什么会发生/应该如何解决会很有趣,所以我会在这里保留。) I have a searchbar with a submit and a dropdown option i am appending to that submit.(我有一个带有提交的搜索栏和一个附加到该提交的下拉选项。) The submit gets appended twice.(提交被追加两次。) I tried using "one" instead of "on" which only returned the first submit.(我尝试使用“一个”代替“打开”,它仅返回第一个提交。) the problem is i need it to only return the second submit or best case prevent the first submit from happening.(问题是我需要它仅返回第二个提交或最好的情况,以防止第一次提交发生。) Currently my url looks like this: "/...?search=test&Item=1&Item=3" and i only want the "&item=3" as the first variable doesn't get updated.(目前,我的网址看起来像这样:“ / ...?search = test&Item = 1&Item = 3”,我只希望“&item = 3”,因为第一个变量不会更新。) var variableToSend = ''; $('#selector').on('click',function() { variableToSend = $(this).val(); $('form').append('<input type="hidden" id="yourData" name="Item" value="'+ variableToSend +'"/>'); });   ask by mulraf translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can either remove the button type submit or you can return false for the particular button function or e.prevent.default.(您可以删除按钮类型提交,也可以为特定按钮功能或e.prevent.default返回false。)

check this link for more details How to prevent buttons from submitting forms(检查此链接了解更多详细信息如何防止按钮提交表单)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...