Combining two input fields to one, jquery
Im trying to combine the name field, and msg field, and input all values into #msg, but cant quite get it to work
<script type="text/javascript" language="text/javascript">
$('#Docu开发者_Go百科mentCommentsForm_21').bind('submit', function(){
var name = "##" + $('#navn').val() + "##";
var msg = $('#msg').val();
$('#msg').val(name+' '+msg);
});
alert($('#msg').val(name+' '+msg));
</script>
you need to get your alert inside the function:
$('#DocumentCommentsForm_21').bind('submit', function(){
var name = "##" + $('#navn').val() + "##";
var msg = $('#msg').val();
$('#msg').val(name+' '+msg);
alert($('#msg').val(name+' '+msg));
});
If #msg
is not an input, use .text()
instead of .val();
$('#DocumentCommentsForm_21').bind('submit', function(){
var name = "##" + $('#navn').val() + "##";
var msg = $('#msg').val();
$('#msg').text(name+' '+msg);
});
You are putting the "combine fields" functionality into the submit event handler, which is fine, but you don't stop the form from submitting, so you will never see the result of you operation on the original form. If this is your intention, and you just want the alert to show your combined result then Haroldo's approach would pretty much suffice except that you'll want to change the code to this:
$('#DocumentCommentsForm_21').bind('submit', function(){
var name = "##" + $('#navn').val() + "##";
var msg = $('#msg').val();
$('#msg').val(name+' '+msg);
alert($('#msg').val());
});
Otherwise you'll get an alert box saying [object Object]
.
精彩评论