![](/img/trans.png)
[英]How to clear default value of form when the user starts typing (jquery)
[英]Mark when the user starts typing in a form
我想在用戶開始輸入表單時將用戶添加到帖子的author
列表中。
例如,當用戶在表單中鍵入任何字母(或按“輸入”按鈕)時,我會調用@post.authors.create(user_id: current_user.id)
將用戶添加到該特定的作者列表中帖子。 我認為這是不可能實現的,但使用javascript提交ajax請求可能足以實現此方法。 是否有可能在Rails應用程序中實現此類方法? 如果是這樣,有人可以給我一些指示嗎?
您必須為按鍵創建事件處理程序並在首次按下后刪除。
function typeCatch(){
$(this).off("keypress",typeCatch)//remove handler
console.log("User start type");
}
$("$field_id").on("keypress",typeCatch)
對於以下答案,我假設您使用jQuery作為javascript框架運行Ruby on Rails並且當前用戶(= author)已登錄(即會話中的當前用戶ID)。 我正在解釋@Mischa的想法:使用按鍵事件。
因此,正如您所解釋的,您有一個表單來編輯帖子。 我猜你里面有一個textarea,帖子的文字。 例如<textarea id="posting">This is a post</textarea>
。
您的下一個要求是,告訴服務器,“當用戶在表單中鍵入任何字母(或按”輸入“按鈕)時。 因此,我們為“keypress”事件定義了一個事件處理程序。 按下第一個鍵后,應刪除此事件處理程序。 感謝@ user2257149獲取以下代碼(我稍微調整了一下):
function typeCatch(){
$(this).off("keypress",typeCatch) // remove handler
console.log("User start type");
}
$("#post").on("keypress",typeCatch); // add handler
此javascript代碼片段必須低於<textarea>
的定義。
現在我們有一個只觸發一次的事件處理程序。 在這個處理程序中,我們觸發ajax調用讓服務器知道,當前用戶正在編輯當前帖子。 當我們更新post對象中的特定屬性時,我們應該觸發類似以下URL的內容:
PUT http://www.example.com/posts/123/add_current_author
這意味着,我們應該使用HTTP PUT方法更新ID為123的帖子和PostsController中的觸發方法PostsController
。
所以我們將函數typeCatch()
調整為:
function typeCatch(){
$(this).off("keypress",typeCatch)//remove handler
$.ajax({
type: "POST",
url: '/posts/123/add_current_author',
data: JSON.stringify({_method: 'put'})
});
}
(由於大多數瀏覽器不支持PUT方法,Ruby on Rails通過發送POST參數“_method”來欺騙它,我從@Michael Koper那里得到了這個關於Ruby on rails的回答 - 更新ajax上的PUT方法 )
您可能必須將此特殊路由添加到Ruby on Rails(不確定以下內容,我的rails有點生疏):
resources :posts do
collection do
put "add_current_author"
end
end
現在,在PostsController
,您必須定義方法add_current_author
。 我假設你實例@post
是實際的職位(由下式給出確定的:id
)中before_filter
,有你的當前用戶保存在current_user
。 實際上它應該像以下一樣簡單:
def add_current_author
@post.authors.create(user_id: current_user.id)
end
雖然我必須承認,它看起來有點奇怪(這是你的建議)。 我猜,我在“posts”和“users”之間有一個1:n的連接叫做“authors”,並執行以下操作:
@post.authors << current_user
再次:感謝@Micha,@ user2257149和@Michael Koper。 我贊成你的答案。
更新:正如賞金評論中所述,OP希望將當前功能從“用戶在顯示時讀取的標記”更改為“僅當用戶在評論表單中按鍵時標記為已讀”。
所以目前EssayController
有一個類似的show
動作
def show
...
@essay.reader_links.create(reader_id: current_user.id)
...
end
應刪除給定的行,因為當顯示@essay
時不應添加當前用戶。 此外,我在上面定義的<textarea>
是注釋表單中的那個,所以HTML可能看起來像
<html>
...
<form action="essay/123/comment" method="post">
...
<textarea name="comment_text" id="comment"></textarea>
...
</form>
<script type="text/javascript">
function typeCatch() {
...
}
$("#post").on("keypress", typeCatch);
</script>
</html>
最后,動作add_current_author
不應該像我假設的那樣在PostController
但在EssayController
並且路徑分別改變了。
更新2:正如@Yarek T所述,而不是$("#post").on("keypress", typeCatch);
jQuery函數one
可用於:
function typeCatch(){
$.ajax({
type: "POST",
url: '/posts/123/add_current_author',
data: JSON.stringify({_method: 'put'})
});
}
$("#post").one("keypress", typeCatch);
大家都忘記了jQuery的$.one()
函數嗎? 它的功能也可以做$.on()
然后那里面$.off()
var flag = 0;
$(document).on('keypress', function(e) {
if(flag == 0) {
doSomething();
flag = 1;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.