私は2つの非常によく似たアクション(likeとlikeと)に使いたい機能を持っています。私は何かの名前を変更する必要があると確信していますが、オンラインで検索したところ、一見明らかなものは見つけられませんでした。どんな助けでも大変感謝しています。JavaScriptの関数の名前を変更するには
スクリプト私は名前を変更したい:
<script>
$(function() {
// get into the habit of caching the elements you will be re-using
var error = $('.error');
$('.button').click(function() {
error.hide();
// cache all the elements we'll be using
var contactForm1 = $(this).closest('.contact_form1'),
likesid = contactForm1.find('input[name=likesid]'),
likerid = contactForm1.find('input[name=likerid]');
unlikestatus = contactForm1.find('button1[name=unlikestatus]');
if (likesid.val() == '') {
// ...
likesid.focus();
return false;
}
if (likerid.val() == '') {
// ...
likesid.focus();
return false;
}
// easier to use object rather than string
// not sure where likestatus came from so it is ommitted
var data = { likesid: likesid.val(), unlikestatus: unlikestatus.val(), likerid: likerid.val() };
// short-hand method for $.ajax with POST
$.post('simpletest.php', data, function (res) {
// the rest
contactForm.html('<h5 class="message" style="color: #4EB44B;">unliked</h5>')
});
// no need to do any return false
});
});
</script>
あなたが関数やスクリプトの名前を変更したいですか? – mkaatman
私はよくわからない、私は非常にjavascriptに新しいです。私は何かをいくつか変更するだけでこの同じコードを2回使うことができるものを探しています – sicksixstrings
あなたの関数に引数として渡します。 –