gpt4 book ai didi

javascript - 特定按钮类 onClick 之前的某些字段的值未定义?

转载 作者:行者123 更新时间:2023-12-03 12:28:41 25 4
gpt4 key购买 nike

本想测试类似的方法来获取重复单击 UpdateButton 之前的字段值,但它在浏览器控制台中记录了未定义

var $prevID = $(this).prevAll('.UpdateStory').first(".storyID");
var $prevStory = $(this).prevAll('.UpdateStory').first(".currentStory");

var id = $prevID.val();
var story = $prevStory.val();

这是 HTML(客户端 + 服务器端handlebars.js)

<div id="allStories" class="allStories"> </div><!--/allStories-->
<script id="storyTemplate" type="text/x-handlebars-template">

<div class="thisness">
<div class="stories">

<div class="new" id="new">
\{{#each stories}}
<div class="container-fluid">
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<form class="updateNewStoryForm">
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox">
</span>
<input type="hidden" class="storyID" value="\{{ _id }}"/>
<input type="text" class="currentStory" value="\{{ story }}">
<span class="input-group-addon">
<input type="button" class="UpdateStory">
</span>
</div><!-- /input-group -->
</form>
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
\{{/each}}
</div>

</div> <!--/stories-->
</div> <!--/thisness-->

</script>

这是 ajax .put,否则仅更新第一个故事

// UpdateStory button clicks
$(".allStories").on('click','.UpdateStory',function(e) {

e.preventDefault();
console.log('UpdateStory clicked');

// story elements for API that work for only the first item,
// regardless of whichever UpdateStory button is clicked
var id = $( ".storyID" ).val();
var story = $( ".currentStory" ).val();

console.log(id);
console.log(story);

var AjaxPostData = {
id : id,
story : story
};

// if the story field has content
if (story.length != 0) {
console.log('there is a story: ' + story);
// make an ajax call
$.ajax({
dataType: 'json',
data: AjaxPostData,
type: 'put',
url:"http://localhost:4200/api/v1/stories/" + id,
success: refreshNewStories,
error: foundAllNewFailure
});
};

}); // UPDATE

最佳答案

应该是:

var group = $(this).closest(".input-group-addon");
var id = group.prevAll(".storyID").val();
var story = group.prevAll(".currentStory").val();

关于javascript - 特定按钮类 onClick 之前的某些字段的值未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24048322/

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