单击按钮后将字符串设置为“1”

时间:2012-11-12 14:27:06

标签: jquery ajax json wcf

我是一个有两个按钮的jquery对话框。

<div id="generatePinsDialog" title="Generate New PINs">

<input type="button" title="button1" id="button1" />
<br />
<input type="button" title="button2" id="button2" />

在我的js文件中,我想设置字符串vlaue并将其传递给wcf方法。

function OpenGeneratePINsDialog() {
$("#generatePinsDialog").dialog('open');
$.get('/MyServices/myService.svc/GeneratePINs/' + #button1.val()+'/' + #button2.value 

}

WCF方法:

[OperationContract]
    [WebGet(UriTemplate = "/.../GeneratePINs/{button1}/{button2}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    public void GeneratePINs(string button1, string button2)
    {  

这意味着如果单击button1,则传递button1 = 1和button2 = 0             如果单击button2,则传递button2 = 1和button1 = 0

感谢您的帮助。还请帮我纠正button1的值写。

           #button1.Val() or $(#button1.Val())

2 个答案:

答案 0 :(得分:4)

    function OpenGeneratePINsDialog(e) {
        var b1=0, b2=0; 
        if(this.id=="button1"){
             b1=1;
         }else{
             b2 =1;
          }
          $("#generatePinsDialog").dialog('open');
          $.get('/MyServices/myService.svc/GeneratePINs/' + b1 +'/' + b2, function(response){

          });
      }
      $("#button1").click (OpenGeneratePINsDialog);
      $("#button2").click (OpenGeneratePINsDialog);

答案 1 :(得分:1)

你可以这样做:

$('input[type=button]').click(function(evt) {
    // setup default values for button (button2 clicked)
    var button1 = "0";
    var button2 = "1";
    // change to button 1 if that was the button pressed
    if (evt.target.id == 'button1') {
        button1 = "1";
        button2 = "0";
    }
    // execute the rest of your code
    $("#generatePinsDialog").dialog('open');
    // append the values of button1 and button2
    $.get('/MyServices/myService.svc/GeneratePINs/' + button1 + '/' + button2);
})

注意您的$.get功能不完整