admin管理员组文章数量:1326287
is it possible to send a value from button to Jquery AJAX with onclick function ? I have tried something like this code, but not working. There is nothing on console log.
html
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="1">x</button>
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="2">x</button>
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="n">x</button>
script
function hapus_krs() {
var formData = {
'id_mhs': $(this).val()
};
$.ajax({
type: 'POST',
url: '<?=base_url()?>akademik/hapus_krs',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
})
};
is it possible to send a value from button to Jquery AJAX with onclick function ? I have tried something like this code, but not working. There is nothing on console log.
html
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="1">x</button>
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="2">x</button>
<button type="button" class="hapus_krs" onclick="hapus_krs(this.value)" value="n">x</button>
script
function hapus_krs() {
var formData = {
'id_mhs': $(this).val()
};
$.ajax({
type: 'POST',
url: '<?=base_url()?>akademik/hapus_krs',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
})
};
Share
Improve this question
asked Jul 20, 2017 at 3:46
Billy AdelphiaBilly Adelphia
1,0274 gold badges19 silver badges34 bronze badges
3 Answers
Reset to default 2When you invoke the function with onclick="hapus_krs(this.value)"
, you are already passing the button value to the function as parameter:
function hapus_krs(value) {
var formData = {
'id_mhs': value
};
}
Unobtrusive JS version:
<button type="button" class="hapus_krs" value="1">x</button>
<button type="button" class="hapus_krs" value="2">x</button>
<button type="button" class="hapus_krs" value="n">x</button>
$('button.hapus_krs').click(function() {
var formData = {
'id_mhs': this.value // $(this).val() also works but it's unnecessary to invoke another jQuery call
};
});
Here you go with the solution https://jsfiddle/60qu6mev/
$('.hapus_krs').click(function(){
var formData = {
'id_mhs': $(this).attr('value')
};
console.log(formData);
$.ajax({
type: 'POST',
url: '<?=base_url()?>akademik/hapus_krs',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
})
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="hapus_krs" value="1">x</button>
<button type="button" class="hapus_krs" value="2">x</button>
<button type="button" class="hapus_krs" value="n">x</button>
I have removed the onclick from HTML, rather used a jQuery click event to handle ajax call & retrieve value from button
You have to be careful when playing with this
. In your code, this
might not refer the the button that has been clicked.
In addition, you are passing a this.value
as a parameter and yet you are not using it. So your function should look like the following:
function hapus_krs(value) {
var formData = {
'id_mhs': value
};
$.ajax({
type: 'POST',
url: '<?=base_url()?>akademik/hapus_krs',
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
})
};
本文标签: javascriptsend button value to Jquery AJAX with onclick()Stack Overflow
版权声明:本文标题:javascript - send button value to Jquery AJAX with onclick() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210027a2433539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论