我如何将textarea值发送到Web服务?

时间:2014-02-20 11:58:25

标签: javascript jquery html web-services

我试图使用这个文本区域和两个按钮将textarea值发送到Web服务我可以做什么来从Web服务调用方法?我是否可以从onclick""打电话,或者我必须把它放在form中,我需要的其他网络服务链接到这个项目下面完成的是我的代码:

<html>
  <head>
    <title></title>
  </head>
  <body>
    <div>
      <div style="width:300px; height:100px;">
        <div style="width:300px; height:80px;"></div>
        <div style="width:300px; height:20px; background-color:#7C728E;"></div>
      </div>
      <div style="width:300px; height:70px">
        <textarea style="width:300px; height:70px; resize: none;" name="tb" type="text" maxlength="500"
        placeholder="Enter text message"></textarea>
      </div>
      <div style="width:300px; height:70px; background-color:#7C728E;">
        <div style="width:87px;margin-left: 3px;">
          <input id="cancel" onclick="****" type="button" value="CANCEL"
          style="background-color:#7C728E;margin-left: -4px; color:#FFFFFF;" />
        </div>
        <div style="width:72px; margin-left: 215px; margin-top: -25px;">
          <input id="send" onclick="*****" type="button" value="SEND"
          style="background-color:#7C728E;margin-left: -4px;color:#FFFFFF;" />
        </div>
      </div>
    </div>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

<html>
  <head>
    <title></title>
  </head>
  <body>
    <div>
      <div style="width:300px; height:100px;">
        <div style="width:300px; height:80px;"></div>
        <div style="width:300px; height:20px; background-color:#7C728E;"></div>
      </div>
      <div style="width:300px; height:70px">
        <textarea style="width:300px; height:70px; resize: none;" name="tb" id="tbContent" type="text" maxlength="500"
        placeholder="Enter text message"></textarea>
      </div>
      <div style="width:300px; height:70px; background-color:#7C728E;">
        <div style="width:87px;margin-left: 3px;">
          <input id="cancel" onclick="return submitData('0')" type="button" value="CANCEL"
          style="background-color:#7C728E;margin-left: -4px; color:#FFFFFF;" />
        </div>
        <div style="width:72px; margin-left: 215px; margin-top: -25px;">
          <input id="send" onclick="return submitData('1')" type="button" value="SEND"
          style="background-color:#7C728E;margin-left: -4px;color:#FFFFFF;" />
        </div>
      </div>
    </div>
  </body>
</html>

<script language="javascript">

 function submitData(isSubmit) 
{
var Msg = $("#tbContent").val();
   if(isSubmit==1)
   {
              $.ajax({
            type: "POST",
            url: "../WebService/yourfile.asmx/yourmethodname",
            data: "{'Msg':'" + Msg}",
            contentType: "application/json",
            async: false,
            success: function (data) {
                if (data.d > 0) {

                        alert("Data saved successfully.");

                }
            }
        });
   }
   else
   {
      //do your cancel job here
    }
}



</script>

答案 1 :(得分:0)

首先创建一个表单。并且这个代码应该起作用:

   $(document).ready(function(){
        $('form').submit(function( e ) {
            var postData = $(this).serializeArray();
            $.ajax({
                type: 'post',
                url: 'form.php', // change to the right file
                data: postData,
                success: function () {
                  //do function on success
                }
            });
            e.preventDefault();
        });
    });  

编辑: 您还需要将semd按钮更改为type="submit js小提琴:http://jsfiddle.net/BcBM5