简体   繁体   English

无法通过ajax将带有单选按钮数据的额外表单数据传递给php

[英]Can't pass extra form data with radio button data to php via ajax

I have a simple form I have a radio button and 2 hidden fields. 我有一个简单的表单,我有一个单选按钮和2个隐藏字段。 I want to pass the radio button value and the hidden fields value via ajax to a php page. 我想通过ajax将单选按钮值和隐藏字段值传递给php页面。 The radio button value passes fine but not the hidden fields do not. 单选按钮的值可以通过,但隐藏字段不能通过。 I have hard coded the the values for table and id in the php page to make sure the value of the radio button is being passed for testing. 我已经在php页面中对表和id的值进行了硬编码,以确保将单选按钮的值传递给进行测试。

<input type="radio" name="contract" value="0" class="custom-switch-input">
<input type="radio" name="contract" value="1" class="custom-switch-input">
<input type="radio" name="contract" value="2" class="custom-switch-input">

<input type="hidden" name="table" value="timber_sales">
<input type="hidden" name="id" value="177">

$(document).ready(function(){
   $('input[type="radio"]').click(function(){
        var contract = $(this).val();

        $.ajax({
             url:"edit_do.php",
             method:"POST",
             data:{contract:contract,id:id,table:table},
        });
   });
});

You're setting the value of contract in your event listener, but not id or table . 您是在事件监听器中设置合同的值,而不是idtable

To get this to work, the way you currently have it, you'll need to search the dom for the value of your hidden fields as well. 为了使它按当前的方式运行,您还需要在dom中搜索隐藏字段的值。

var contract = $(this).val();
var id = $('input[name="id"]').val();
var table = $('input[name="table"]').val(); 

You need to get the values from the hidden fields first. 您需要首先从隐藏字段中获取值。 Just like you did with $(this).val() for the value of the radio button. 就像您使用$(this).val()获取单选按钮的值一样。 So: 所以:

$(document).ready(function(){
   $('input[type="radio"]').click(function(){
        var contract = $(this).val();
        var id = $('input[name=id]').val();
        var table = $('input[name=table]').val();

        $.ajax({
             url:"edit_do.php",
             method:"POST",
             data:{
               contract: contract,
               id:id,
               table:table
             },
        });
   });
});

It is probably better to give the hidden fields an unique ID attribute and get them through $('#myuniqueid').val() , but at least the above work as long as you don't have any other fields named ID or table. 最好给隐藏字段一个唯一的ID属性,并通过$('#myuniqueid').val()获取它们,但是至少在没有其他名为ID或表的字段的情况下,至少可以完成上述工作。

Your jquery event does not know about hidden fields. 您的jquery事件不知道隐藏字段。 Try this: 尝试这个:

            <form id="form1">
                <input type="radio" name="contract" value="0" class="custom-switch-input">
                <input type="radio" name="contract" value="1" class="custom-switch-input">
                <input type="radio" name="contract" value="2" class="custom-switch-input">

                <input type="hidden" name="table" value="timber_sales">
                <input type="hidden" name="id" value="177">
            </form>
            <form id="form2">
                <input type="radio" name="contract" value="0" class="custom-switch-input">
                <input type="radio" name="contract" value="1" class="custom-switch-input">
                <input type="radio" name="contract" value="2" class="custom-switch-input">

                <input type="hidden" name="table" value="timber_sales">
                <input type="hidden" name="id" value="177">
            </form>
            $(document).ready(function(){
                $('form').change(function(){
                    event.preventDefault();

                    var send = $(this).data();

                    $.ajax({
                        url:"edit_do.php",
                        method:"POST",
                        data:send,
                    });
                });
            });    

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

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