开发者

jQuery: having radio buttons and a textarea update hidden form fields

Really new to using jQuery and trying to find an example I need.

1) if I have, say, 5 radio buttons to choose an item, how do I pass the selected item to a hidden form field?

2) same question for a textare开发者_开发技巧a. How do I pass the text written to a hidden form field and make sure it's escaped safely for a form submission?

Thanks for any help.


You can just bind to the change event:

<input type="hidden" id="myradiovalue" />
<input type="radio" name="myradio" value="0" />
<input type="radio" name="myradio" value="1" />

$('input[name=myradio]').change(function() {
    $('#myradiovalue').val($(this).val());
});

And almost the same for textarea:

<input type="hidden" id="mytextarevalue" />
<textarea id="mytextareavalue"></textarea>

$('textarea').change(function() {
    $('#mytextareavalue').val($(this).val());
});


For both <input type="radio"> and <textarea>, you will want to use jQuery change() method. If you want to sanitize the input before it is inserted into a <input type="hidden"> then you will need to use some regex or a library that does it for you, like jQuery Validation Plugin. Keep in mind that any sanitation/validation you do with javascript/jQuery will need to be double-checked server-side after the form is submitted.

But I don't know why you are copying data from one form input to another, can't you just use the form input as it is? What is the point of having the data in both a <textarea> and a <input type="hidden">?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜