开发者

Grab multiple input radio values and Submit to PHP script

well today I want to learn how to grab multiple input type button value compare to original value and last submit to php script! How can I do that with Jquery?

<html>
<body>
<div id="leftDiv" class开发者_JS百科="container"> 
<form action="" method="get"> 
    <fieldset>
        <p>Something 0</p>

        <lable>Yes</label>
        <input type="radio" name="Group0" value="1" >

        <lable>No</label>
        <input type="radio" name="Group0" value="0" >

    </fieldset>

    <fieldset>
        <p>Something 1</p>

        <lable>Yes</label>
        <input type="radio" name="Group1" value="1" >

        <lable>No</label>
        <input type="radio" name="Group1" value="0" >

    </fieldset>

    <fieldset>
        <p>Something 2</p>

        <lable>Yes</label>
        <input type="radio" name="Group2" value="1" >

        <lable>No</label>
        <input type="radio" name="Group2" value="0" >

    </fieldset>

    <fieldset>  
        <input type="submit" name="update" class="button" value="Submit"> 
    </fieldset>
</form>
</div>
</body>
</html>

the above values are generated by php script (can be 1 or 100)


the easiest method is actually to set the correct values as selected in the HTML and tie a .onChange event to them. otherwise, have a hidden field with the original value, and onChange compare them like this

   //a hidden input field with the original value
    <input type='hidden' id='Group0' value='0'/>
    // a set of radio fields that reference the original field we will be comparing with in their 'rel' attribute for easy jQuery selection
    Off<input type='radio' name='Group0' rel='#Group0' class='compareOnChane' value=0/><br/>
    On <input type='radio' name='Group0' rel='#Group0' class='compareOnChane' value=1/>

    <script>
    $(document).ready(function(){
       $('.compareOnChange').live('change',function(){
         if($($(this).attr('rel')).val()!=$(this).val(){
          //the value currently is different than the original value, fire our action
         }

       });

    });

    </script>


This is a pretty good tutorial that will teach you the basics of this:

http://trevordavis.net/blog/ajax-forms-with-jquery

You can get the values of the radio buttons in a JSON like so:

var radios = {};
$('input[type="radio"]').each(function() {
  radios[$(this).attr('name')] = $(this).attr('value');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜