2

I have created a button on my page that its HTML looks like this:

enter image description here

Now in my JavaScript I have a jQuery that finds that button like this:

 $('.personlistbtn').click(function(e) {
    console.log("inside click event of person list");
    console.log("provider id was this " + provider_id);
    // ????? 
  });

I want to attach some parameters to my href button that posted above so it can go to a new page in my Rails app with those parameters I am passing to it so hopefullu something like this:

<a href="/pharmacy/patients?provider_id=234" >

Notice I attached that ?provider_id=234 to it. How can I do that?

4

3 回答 3

5

Assuming provider_id is already set elsewhere in your code, this should work:

$('.personlistbtn a').click(function(e) {
    e.preventDefault();
    window.location.assign(this.href + '?provider_id=' + provider_id);
});     
于 2013-08-09T13:44:17.093 回答
1

You have to edit the href attr:

 $('.personlistbtn').on('click', function() {
     var $a = $(this).find('a');
     var href = $a.attr('href');
     $a.attr('href', href + '?provider_id=234');
 });

EDIT:

That code will work when clicking on the container div. If you want to attach the parameters when clicking on the anchor, you should do the following:

 $('.personlistbtn').on('click', 'a', function(e) {
     e.preventDefault();
     window.location = $(this).attr('href') + '?provider_id=234';
 });
于 2013-08-09T13:45:57.420 回答
0

Try this:

$('.personlistbtn a').click(function (e) {
    e.preventDefault();
    var provider_id = '?provider_id=234'; // this you already have, I just put it to have a value
    var newurl = this.href + '?provider_id=' + provider_id
    window.location.assign(newurl);
});

JSFIDDLE

于 2013-08-09T14:00:47.930 回答