开发者

Combining two input fields to one, jquery

开发者 https://www.devze.com 2022-12-27 10:11 出处:网络
Im trying to combine the name field, and msg field, and input all values into #msg, but cant quite get it to work

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].

0

精彩评论

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

关注公众号