开发者

Get value from checkbox - send it to a form (Hidden field)

How can I pass values from checkboxes in to a form on my website using jQuery?

Example

When a checkbox is checked, jQuery will MAKE a new input field, inside a specific fo开发者_如何学Crm.

Thanks


$('#checkbox_field_id').click(function() {
    var checkbox_field_value = $(this).val();
    $('#hidden_input_id').val(checkbox_field_value);
});


If you want JQuery to create a new hidden field you can do it like this:

$('#idOfForm').append($('<input>').attr('id', 'idOfHiddenField').attr('value', 'valueOfHiddenField').attr('name', 'nameOfValue').attr('type', 'hidden');


How about this?

$("#myCheckbox").click(function(){
    var $form = $("#myForm");
    var $this = $(this);
    var $hiddenField = $this.data("hiddenField")
    if(!$hiddenField){
        $hiddenField = $form.append('<input type="hidden" />')
        $this.data("hiddenField", $hiddenField);
    };
    $hiddenField.val(this.value);
});

In this code you can be sure the input field only gets appended the first time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜