简体   繁体   English

单击单选按钮时的jQuery帖子

[英]jQuery post when click on radio button

I have problem about jQuery post in radio button. 我对单选按钮中的jQuery post有问题。 I can't save radio button value when I click radio button using jQuery AJAX post 当我使用jQuery AJAX发布单击单选按钮时,无法保存单选按钮值

Here's my form: 这是我的表格:

<?php
  $jaw = mysql_query("SELECT * FROM jwb");
  $j=mysql_fetch_array($jaw);
?>
<div id="id_jwb"><?php echo $j['id_jwb'];?></div><?php echo $j['jwb'];?><br>
<input type="radio" name="jwb1" value="A" id="jwb1" <?php if($j['jwb']=='A'){echo 'checked';}?>>A<br>
<input type="radio" name="jwb1" value="B" id="jwb1" <?php if($j['jwb']=='B'){echo 'checked';}?>>B<br>
<input type="radio" name="jwb1" value="C" id="jwb1" <?php if($j['jwb']=='C'){echo 'checked';}?>>C<br>

And here's my jQuery post: 这是我的jQuery帖子:

$("#jwb1").click(function(){
                var jawaban = $("input[name=jwb1]:checked").val();
                var id_jawaban = $("#id_jwb").html();
                $.post('update_jwb.php',{ jwb: jawaban, id_jwb: id_jawaban });
});

And here's my php file to insert db: 这是我插入db的php文件:

$con = mysql_connect($host, $username, $password);
mysql_select_db($database, $con);

$jwb = $_POST['jwb']; // $_POST['jwb'] dari jQuery-post
$id = $_POST['id_jwb'];

$sql = "UPDATE jwb SET jwb='{$jwb}' WHERE id_jwb='$id'";
$query = mysql_query($sql, $con);
if($query){
    echo 'sukses';
}
else{
    echo 'gagal';
}

The problem is with your html/jQuery. 问题出在您的html / jQuery上。 You are trying to use the id of the radio button however all three radio buttons have the same id value so the browser doesn't know which value you want it to grab. 您正在尝试使用单选按钮的ID,但是所有三个单选按钮都具有相同的ID值,因此浏览器不知道您要获取哪个值。 You can fix this by using the following jQuery which uses input name instead of the id and only gets the value of the radio button that is checked.: 您可以使用以下jQuery来解决此问题,该jQuery使用输入名称而不是id并仅获取选中的单选按钮的值。

$("input[name=jwb1]").click(function(){
    var jawaban = $("input[name=jwb1]:checked").val();
    var id_jawaban = $("input[name=jwb1]:checked").html();
    $.post('update_jwb.php',{ jwb: jawaban, id_jwb: id_jawaban });
});

Also I just noticed that these inputs don't have any inner HTML therefore... 我也注意到这些输入没有任何内部HTML,因此...

$("input[name=jwb1]:checked").html()

...won't return anything so the "id_jwb" post value will always be empty. ...将不会返回任何内容,因此“ id_jwb”发布值将始终为空。 That means you can pretty much get rid of this post value. 这意味着您几乎可以摆脱这个职位价值。

If you want to submit a value AND id of the radio button that is selected then you would need to make every radio button have a unique id. 如果要提交所选单选按钮的值AND ID,则需要使每个单选按钮具有唯一的ID。 Then use the following jQuery: 然后使用以下jQuery:

$("input[name=jwb1]").click(function(){
    var jawaban = $("input[name=jwb1]:checked").val();
    var id_jawaban = $("input[name=jwb1]:checked").attr("id");
    $.post('update_jwb.php',{ jwb: jawaban, id_jwb: id_jawaban });
});

Each id value must be used only once within a document. 每个id值在文档中只能使用一次。 If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. 如果为多个元素分配了相同的ID,则使用该ID的查询将仅选择DOM中的第一个匹配元素。 This behavior should not be relied on, however; 但是,不应依赖这种行为。 a document with more than one element using the same ID is invalid. 包含多个使用相同ID的元素的文档无效。

Hence I suggest you to use class with input tag and use class selector for attaching a click event. 因此,我建议您使用带有输入标记的类,并使用类选择器来附加click事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM